如何使routelink返回正确的URL? [英] How to make routelink return the correct URL?
问题描述
这是复制问题的步骤.
- 开始新的mvc 4项目.
-
在默认"路由之前,将以下几行添加到RegisterRoutes()方法中
- start new mvc 4 project.
add the following lines to the RegisterRoutes() method just before the "Default" route
routes.MapRoute(
name: "Test",
url: "Test/{action}",
defaults: new { controller = "Test", action = "Index" }
);
将以下内容添加到Index.cshtml和About.cshtml
add the following to Index.cshtml and About.cshtml
@Html.RouteLink("link to TestController", "Test")
如您所见,这两个页面生成的html代码是不同的.我希望URL是为Index.cshtml正确呈现的"/Test",但对于About.cshtml,它将变为"/Test/About".
As you can see that the generated html code is different for the 2 pages. I would expect the URL is "/Test" which is rendered correctly for Index.cshtml, but it become "/Test/About" for About.cshtml.
我的问题是,是否有任何记录的方式来生成相同的正确URL.上面的示例仅用于演示目的.我想使用路由表信息来生成菜单.
My question is if there is any documented way to generate the same correct URL. The above example is created just for demonstration purpose. I would like to use the routing table information to generate menu.
推荐答案
似乎引擎正在从当前页面设置"action
"路由值(索引",关于").因此,解决此问题的一种方法是重置"该路由值:
It seems the engine is setting the action
route value from the current page ("Index", "About"). So one way to fix this is to "unset" that route value:
@Html.RouteLink("link to Test Controller", "Test", new { action = String.Empty })
这篇关于如何使routelink返回正确的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!