Asp.Net MVC: Reusing values for segment variables when generating outbound URLs

最后都变了- 提交于 2019-12-11 08:11:32

问题


When routes are matched to the outbound URLs, the routing system will try to find values for each of the segment variables in route's URL pattern by also looking at the values from the current request. If necessary, routing system will reuse segment variable values from the incoming URL

Excerpt is from Pro Asp.Net MVC 4 book:

The routing system will reuse values only for segment variables that occur earlier in the URL pattern than any parameters that are supplied to the Html.ActionLink method. Suppose we tried to create a link like this:

@Html.ActionLink("Click me", "List", "Catalog", new {color="Aqua"}, null)

We have supplied a value for color, but not for page. But color appears before page in the URL pattern and so the routing system won’t reuse the values from the incoming URL, and the route will not match.

... We strongly recommend that you do not rely on this behavior and that you supply values for all of the segment variables in a URL pattern. By relying on this behavior you end up making assumptions about the order in which your users make requests

Note: the route to which the excerpt is referring to is defined as :

 routes.MapRoute("MyRoute", "{controller}/{action}/{color}/{page}");

a)

The routing system will reuse values only for segment variables that occur earlier in the URL pattern than any parameters that are supplied to the Html.ActionLink method

What could go wrong if routing system also reused values for segment variables that occur later in the URL pattern than parameters supplied to Html.ActionLink?

b)

We strongly recommend that you do not rely on this behavior and that you supply values for all of the segment variables in a URL pattern. By relying on this behavior you end up making assumptions about the order in which your users make requests

I don't understand how not supplying values for all segment variables translates into making assumptions about the order in which users make requests?!

Much appreciated

来源:https://stackoverflow.com/questions/15483451/asp-net-mvc-reusing-values-for-segment-variables-when-generating-outbound-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!