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

560 Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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" 
    });
1
On

Does this not work?

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

The routing engine should be able to turn that into the correct URL based on your routing tables.