Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Routing get rid of /Index in URL

I have a dynamic list of ActionLinks generated at run time like this:

@Html.ActionLink(x.Name, "Index", "User", new { x.ID }, null)

so I'm hitting the Index method on the User controller.

I also have my RouteConfig.cs (it's an MVC4 app) set as follows:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

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

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

This gives me the link server/application/User/Index/1 in my list of links.

What do I need to do in the list generation and / or routing file to change this to server/application/User/1?

like image 606
markp3rry Avatar asked Nov 26 '12 15:11

markp3rry


1 Answers

Add a route that doesn't need an action but won't clash with other routes

routes.MapRoute("User", "User/{id}", 
                         new { controller = "User", action = "Index" });
like image 86
dove Avatar answered Oct 16 '22 20:10

dove