嗨,大家可能会帮助我理解为什么@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都会产生错误的网址。
当前网址 http://localhost:2288/Catalog/a/b/c/d/e
所有 ActionLinks和RouteLinks(exept ActionLink5和RouteLinks5)正在生成错误的网址。
我在这里放了一个示例项目:http://www.mediafire.com/?gdbatoafgd0kf4w
可能有人可以弄清楚为什么会这样?
这个故事是几天前开始的,当我尝试使用MvcSiteMapProvider
构建Breadcrumbs时,我得到MvcSiteMapProvider Breadcrumbs
生成的相同链接。
在我的研究期间,我发现MvcSiteMapProvider
没有在其他地方引起问题。所以我创建了默认的MVC4项目,默认情况下它有这种奇怪的行为。
更新
看起来好像当您使用基于当前网址生成的@Html.ActionLink
和@Html.RouteLink
个助手网址时,但仍然无法理解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
答案 0 :(得分:6)
RouteLink允许您明确指定路径名称。 ActionLink按路径定义中出现的顺序评估路径。您的第一个路由声明无效,这是ActionLink帮助程序无法捕获它的原因。您的第一个路由定义无效的原因是您将{a}
,{b}
,{c}
和{d}
路由参数设为UrlParameter.Optional
,但仅限最后参数(示例中为{e}
)可以是可选的。
由于RouteLink
帮助器允许您明确说明要拾取的路由,因此可以将其用作解决方法。