MVC4 路由 @Html.ActionLink 和 @Html.RouteLink 產生錯誤的 url
也許有人可以幫助我理解為什麼
@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/dActionLink1、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 urlhttp://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
RouteLink 允許您顯式指定路由名稱。ActionLink 按照路由定義中出現的順序評估路由。您的第一個路由聲明無效,這也是 ActionLink 助手無法擷取它的原因。您的第一個路由定義無效的原因是因為您將
{a},{b}和route 參數設置為但只有 uri 路徑的LAST參數(在您的範例中)可以是可選的。{c}``{d}``UrlParameter.Optional``{e}由於
RouteLinkhelper 允許您明確說明要選擇哪條路線,因此您可以將其用作解決方法。