简体   繁体   English

从ASP.NET MVC路由生成URL

[英]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? 如何生成使用路由路径而不是实际控制器/操作路径的URL?

Eg 例如

Url.Action("Name", "Profile")

Should generate "Public/DataCapture/Name" rather than "Public/Profile/Name" 应该生成“ Public / DataCapture / Name”,而不是“ Public / Profile / Name”

Try using Url.RouteUrl . 尝试使用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. 过去,当我遇到问题,无法返回正确的路线时,通常表明我的路线未按正确的顺序定义,或者我做的事情使路由系统无法匹配我想要的路线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM