Search code examples
asp.net-mvcstaticasp.net-mvc-3url-routingurl.action

How should I add a static value to the end of a route using Url.Action (in ASP.NET MVC 3)?


I have this route:

routes.MapRoute(null, "Users/{id}/Summary", new { controller = "Users", action = "GetSummary" });

How can I specify this using a Url.Action?

I'm currently using:

string path = Url.Action("Index", "Users", new { id = user.Id } ) + "/Summary";

Is there a cleaner approach?


Solution

  • Why are you using Index when the action is GetSummary?

    string path = Url.Action("GetSummary", "Users", new { id = user.Id } );
    

    You probably want to give your Route a name

    routes.MapRoute("GetSummary", "Users/{id}/Summary", 
        new 
        {
            controller = "Users", 
            action = "GetSummary" 
        });