也许有人可以帮助我理解为什么@Html.ActionLink
并 @Html.RouteLink
在某些情况下产生错误的链接。
我有这样声明的路线:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Catalog", // Route name
"Catalog/{a}/{b}/{c}/{d}/{e}", // URL with parameters
new { controller = "Catalog", action = "Index", a = UrlParameter.Optional, b = UrlParameter.Optional, c = UrlParameter.Optional, d = UrlParameter.Optional, e = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
我有我的参数控制器:
public ActionResult Index(string a, string b, string c, string d, string e)
{
//Do stuff;
}
我有这样声明的视图@Html.RouteLink
和视图的案例列表:@Html.ActionLink
@Html.ActionLink("ActionLink1","Index", new { a = "a" }, null)
@Html.ActionLink("ActionLink2","Index", new { a = "a", b = "b" }, null)
@Html.ActionLink("ActionLink3","Index", new { a = "a", b = "b", c = "c" }, null)
@Html.ActionLink("ActionLink4","Index", new { a = "a", b = "b", c = "c", d = "d" }, null)
@Html.ActionLink("ActionLink5","Index", new { a = "a", b = "b", c = "c", d = "d", e = "e" }, null)
<br/>
@Html.RouteLink("RouteLink1","Catalog", new { a = "a" })
@Html.RouteLink("RouteLink2","Catalog", new { a = "a", b = "b" })
@Html.RouteLink("RouteLink3","Catalog", new { a = "a", b = "b", c = "c" })
@Html.RouteLink("RouteLink4","Catalog", new { a = "a", b = "b", c = "c", d = "d" })
@Html.RouteLink("RouteLink5","Catalog", new { a = "a", b = "b", c = "c", d = "d", e = "e"
})
结果
当前网址 http://localhost:2288/Catalog
ActionLink1
- 实际结果:
http://localhost:2288/Catalog?a=a
- 预期结果:
http://localhost:2288/Catalog/a
RouteLink1
- 实际结果:
http://localhost:2288/Catalog
- 预期成绩:
http://localhost:2288/Catalog/a
当前网址 http://localhost:2288/Catalog/a
ActionLink1
- 实际结果:
http://localhost:2288/Catalog?a=a
- 预期成绩:
http://localhost:2288/Catalog/a
当前网址 http://localhost:2288/Catalog/a/b
ActionLink1
- 实际结果:
http://localhost:2288/Catalog/a/b
- 预期成绩:
http://localhost:2288/Catalog/a
RouteLink1
- 实际结果:
http://localhost:2288/Catalog/a/b
- 预期成绩:
http://localhost:2288/Catalog/a
当前网址 http://localhost:2288/Catalog/a/b/c
ActionLink1
- 实际结果:
http://localhost:2288/Catalog/a/b/c
- 预期成绩:
http://localhost:2288/Catalog/a
RouteLink1
- 实际结果:
http://localhost:2288/Catalog/a/b/c
- 预期成绩:
http://localhost:2288/Catalog/a
ActionLink2
- 实际结果:
http://localhost:2288/Catalog/a/b/c
- 预期成绩:
http://localhost:2288/Catalog/a/b
RouteLink2
- 实际结果:
http://localhost:2288/Catalog/a/b/c
- 预期成绩:
http://localhost:2288/Catalog/a/b
等等...
当前网址 http://localhost:2288/Catalog/a/b/c/d
ActionLink1、ActionLink2 和 ActionLink3 以及 RouteLink1、RouteLink2 和 RouteLink3 都生成错误的 url。
当前网址 http://localhost:2288/Catalog/a/b/c/d/e
所有ActionLinks 和 RouteLinks(除了 ActionLink5 和 RouteLinks5)都产生了错误的 url。
我在这里放了一个示例项目:http ://www.mediafire.com/?gdbatoafgd0kf4w
可能有人可以弄清楚为什么会这样?
那个故事开始于几天前,当时我试图用 构建面包屑MvcSiteMapProvider
,我得到了相同的链接MvcSiteMapProvider Breadcrumbs
。在我的研究中,我发现MvcSiteMapProvider
不会在其他地方引起问题。所以我创建了默认的 MVC4 项目,默认情况下它有这样奇怪的行为。
更新
看起来当您使用@Html.ActionLink
和@Html.RouteLink
基于当前 url 生成的帮助程序 url 时..但是当Current url
http://localhost:2288/Catalog
我得到时仍然不明白为什么:
http://localhost:2288/Catalog?a=a
而不是http://localhost:2288/Catalog/a
在ActionLink
而http://localhost:2288/Catalog
不是http://localhost:2288/Catalog/a
在RouteLink