1

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

5
  • The verbage is confoozing, but the message, in effect is, don't supply page while leaving color null. It's ok to supply just Controller and leave all else null. Don't expect to be able to give null controller but pass specific action. Feel free to give Action and Controller, but don't try leaving Action Null while passing color. Finally, don't give page while leaving anything previous to it empty. The routing engine accomodates nulls, but a null is the end of the url. Dont expect to pass a url like /Home/Index//5 Commented Mar 18, 2013 at 17:53
  • @Dave A: I think you've misunderstood my question, since I do understand how the routing system works, but I'd also like to know what could go wrong if routing system was allowed to also reuse values for segment variables that occur later in the URL pattern than parameters supplied to Html.ActionLink Commented Mar 18, 2013 at 19:11
  • I may still be missing the point, but seems like same issue. ActionLink creates a link (eg /home/index/Aqua/1). If you passed values for page and not color, the resulting link would be what? would it be /home/index//1? Commented Mar 18, 2013 at 19:21
  • @Dave A: If you passed value for page and not color, then routing system would reuse Color's segment variable value from incoming URL. Thus, if incoming URL was /Home/Index/Blue/100 and we had ActionLink("Click","NewAction","NewController", new{page=1000}, null), then outbound URL would be /NewController/NewAction/Blue/1000 Commented Mar 18, 2013 at 20:17
  • Good point. Hadn't thought of it that way, but you're right that the routing system re-uses routes... Commented Mar 18, 2013 at 20:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.