开发者

Generating URLs from ASP.NET MVC routes

开发者 https://www.devze.com 2023-02-24 13:32 出处:网络
If I have some routes set up as follows: context.MapRoute(\"Route1\", \"Public/DataCapture/Name\", new { controller = \"Profile\", action = \"Name\" } );

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消