【发布时间】:2016-01-14 12:11:02
【问题描述】:
在我的 MVC 项目中,我有 Item 控制器和一些动作,例如 Index。
RouteConfig 包括:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
在某些视图中,我使用辅助方法 Html.ActionLink("Items","Index","Item") 为索引操作创建锚点。所以锚结果的 href 将是 (/Item/Index)
现在,我需要映射以下 static URL:
/IndirectItem/索引
到 Item 控制器的 Index 动作,使用默认参数 (indirect = true),因此 RouteConfig 将为:
routes.MapRoute(
name: "IndirectItem",
url: "IndirectItem/Index",
defaults: new { controller = "Item", action = "Index", indirect = true }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
看起来没问题,并且客户端请求已正确映射,但 Html.ActionLink("Items","Index","Item") 辅助方法产生的所有锚点都映射到 URL (/IndirectItem/Index) 而不是 (/Item/Index)。
如何在不将所有 Html.ActionLink() 更改为 Html.RouteLink() 或为原始 url 添加另一个路由的情况下解决此问题?
【问题讨论】:
标签: c# asp.net asp.net-mvc razor asp.net-mvc-routing