从ASP生成url.. NET MVC路由

本文关键字:MVC 路由 NET url ASP 生成 | 更新日期: 2023-09-27 17:50:07

如果我有一些路由设置如下:

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

如何生成使用路由路径而不是实际控制器/操作路径的url ?

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

应该生成"Public/DataCapture/Name"而不是"Public/Profile/Name"

从ASP生成url.. NET MVC路由

尝试使用Url.RouteUrl。

在过去,当我在返回正确的路由时遇到问题时,这通常表明我的路由没有按正确的顺序定义,或者我做了一些事情,使路由系统无法匹配我想要的路由。