Asp.Net MVC: Reusing values for segment variables

2019-08-08 21:10发布

问题:

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