0
votes

I have a project that has three different controllers for tax, Sales, Gas, Utility. When I invoke one from the menu like http://localhost/tax/salestax it nicely goes to the Index method. Route config is like this:

 routes.MapRoute(
     name: "Default",
     url: "{controller}/{action}/{id}",
     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
 );

This works fine when calling the method to make a new entry but fails when calling an entry already entered from a list with the transaction number.

 public ActionResult Index(long? id)
 { 
    if (id = null) 
    {
       return newTaxRequest();
    }
    return existingTaxRequest(id.ToString());
 }

The page will not even break on the Index when called with the parameter:

http://localhost/tax/salestax/500030

The Home controller Index action method is simply:

   public ActionResult Index()
   {
      return View();
   }

How do I configure the controller actions or the route map to fix this?

OK, the senior guy here changed the route to:

 salestax/{id}

with the action of "Index", which does allow me to see a created trans. And he added two more route for the other two types. However, now I cannot create a new one since it always goes back to the Index, in an endless cycle. If I change it to:

 salestax/{action}/{id}

I am able to create a new trans, but unable to see the created one with the error that http://localhost/tax/salestax/500030 cannot be found.

Any other ideas?

2
Agree with @dotnetom, what you posted isn't valid code so it's pretty hard to guess at what might be wrong. Update your post with the actual code you're executing. If the controller isn't too large it would help to see the other methods on the controller as well.Craig W.
If that's the only route configured, then there is no way http://localhost/tax/salestax would go to the Index action of the tax controller. It would invoke salestax action of the tax controller. So, try going to http://localhost/tax/500030 - that should invoke Index action with the parameter. Home controller and it's Index action is not relevant to your problem.Floremin

2 Answers

0
votes

MVC is unable to work out which action should be called as they both potentially have the same signature - your id is nullable and is set as optional in your route set up, so if no id is passed, it doesn't know whether to send the request to Index() or Index(long? id).

You can remove the Index() which doesn't take an id and have both actions served by your Index(long? id) method, just checking for a null value initially.

0
votes

The code you posted uses non-standard methods that are never defined in the question, so to a certain extent it's impossible to know what the issue may be. Also, you mention three controllers, but with only the default route you posted, there's no way the URLs you're using would ever hit any of those. Just as the route says, the URL is going to be interpreted as /{controller}/{action}/{id}, with id being an optional param. So, for a URL like /tax/salestax/500030, the controller named TaxController would be instantiated, and the action SalesTax within that controller would be called, with a parameter of "500030" for id. Which means your code would have to look something like:

public class TaxController : Controller
{
    public ActionResult SalesTax(long? id)
    {
        ...
    }
}

If you truly have 3 separate controllers, then your URL would need to take that into account, i.e. /sales/tax, /gas/tax, /utility/tax, where each would then have an action named Tax. Or, if you wanted them all under the /tax prefix, you could add another route with that:

routes.MapRoute(
    name: "Tax",
    url: "tax/{controller}/{action}/{id}",
    defaults: new { controller = "Sales", action = "Index", id = UrlParameter.Optional }
);

Then, you could have a URL like /tax/sales which would call the Index action of SalesController by default.