也许有人可以帮我理解为什么@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 }
);
}
我有我的参数 Controller :public ActionResult Index(string a, string b, string c, string d, string e)
{
//Do stuff;
}
我有 @Html.RouteLink
的案例列表和 @Html.ActionLink
在这样声明的 View 上: @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
helpers 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
的情况下
最佳答案
RouteLink 允许您明确指定路由名称。 ActionLink 按照路由定义中出现的顺序评估路由。您的第一个路由声明无效,这也是 ActionLink 助手无法捕获它的原因。你的第一个路由定义无效的原因是因为你做了 {a}
, {b}
, {c}
和 {d}
路由参数为 UrlParameter.Optional
但只有 最后 uri 路径的参数(在您的示例中为 {e}
)可以是可选的。
自 RouteLink
helper 允许您明确说明您要选择的路线,您可以将其用作解决方法。
关于asp.net-mvc-4 - MVC4 路由 @Html.ActionLink 和 @Html.RouteLink 产生错误的网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16096716/