Generating URLs from ASP.NET MVC routes
If I have some routes set up as follows:
context.MapRoute("Route1", "Public/DataCapture/Name", new { controller = "Profile", action = "Name" } );
context.MapRoute("Route2",开发者_运维知识库 "Public/DataCapture/Age", new { controller = "Profile", action = "Age" } );
context.MapRoute("Route2", "Public/DataCapture/Amount", new { controller = "Income", action = "Amount" } );
How can I generate URLs that use the route path and not the actual controller/action path?
E.g.
Url.Action("Name", "Profile")
Should generate "Public/DataCapture/Name" rather than "Public/Profile/Name"
Try using Url.RouteUrl.
In the past, when I have had problems getting that to return the correct route, it was usually an indication that either my routes were not defined in the correct order, or I was doing something that kept the routing system from matching the route I intended.
精彩评论