ASP.NET MVC的URL路由与多路径的值 [英] ASP.NET MVC URL Routing with Multiple Route Values
问题描述
我有Html.ActionLink麻烦,当我有需要多个参数的路线。例如,给定在我Global.asax文件中定义的以下路线:
I am having trouble with Html.ActionLink when I have a route that takes more than one parameter. For example, given the following routes defined in my Global.asax file:
routes.MapRoute(
"Default", // Route name
"{controller}.mvc/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
routes.MapRoute(
"Tagging",
"{controller}.mvc/{action}/{tags}",
new { controller = "Products", action = "Index", tags = "" }
);
routes.MapRoute(
"SlugsAfterId",
"{controller}.mvc/{action}/{id}/{slug}",
new { controller = "Products", action = "Browse", id = "", slug = "" }
);
第一两条路线的工作没有问题,但是当我尝试使用创建动作链接到第三条道路:
The first two routes work without a problem, but when I try to create an action link to the third route using:
<%= Html.ActionLink(Html.Encode(product.Name), "Details", new { id = product.ProductId, slug = Html.Encode(product.Name) }) %>
我最终像一个网址 [站点根目录] /详细信息/ 1?蛞蝓= URL-塞,而我想的网址更像 [站点根目录] /细节/ 1 / URL-塞
任何人都可以看到我错了?
Can anyone see where I am going wrong?
推荐答案
据使用的是完全满足了第一条路线。尝试把默认
一个以上的 SlugsAfterId
路线。
It is using the first route that is fully satisfied. Try putting your SlugsAfterId
route above the Default
one.
这基本上会:?。?。检查默认有一个动作是有一个ID是使用这一个,另送查询字符串的任何其他参数
It's basically going: "Check Default. Got an action? Yes. Got an id? Yes. Use this one and chuck any other parameters in the querystring."
作为一个方面说明,这样做,会让你的默认
路线多余的,因为您提供的塞一个默认值
参数。
As a side note, doing that will make your Default
route redundant as you provide a default value for the slug
parameter.
这篇关于ASP.NET MVC的URL路由与多路径的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!