Search code examples
asp.net-mvc-3asp.net-mvc-routingmaproute

Url Mapping: Don't want to have to type Index in the url


I have the following URL map in Global.asax.cs:

 routes.MapRoute("RejectRevision", "{controller}/{Index}/{revisionId}"
        , new { controller = "RejectRevision", action = "Index", revisionId = "" });

But I don't want to have to type http://localhost:9999/RejectRevision/Index/1, I want to be able to type in http://localhost:9999/RejectRevision/1 in order to hit the Index action on the RejectRevision controller. What am I missing here?

THanks.


Solution

  • Put this before your Default route:

    routes.MapRoute(
        "RejectRevision",
        "{controller}/{revisionId}",
        new { 
            controller = "RejectRevision", 
            action = "Index", 
            revisionId = UrlParameter.Optional }
    );
    

    If this is placed before your Default route, a request of /RejectRevision/1 will map to RejectRevisionController.Index() action method.

    Or, if this is the only Controller/Action method you want to be mapped like this, then you can use a literal instead of a parameter for the route:

    routes.MapRoute( 
        "RejectRevision", 
        "RejectRevision/{revisionId}", 
        new {  
            controller = "RejectRevision",  
            action = "Index",  
            revisionId = UrlParameter.Optional } 
    );