Reputation: 10995
This has probably been asked already - if so sorry! I couldn't find it. I am unsure as to how asp is able to decide when to use a query string and "normal looking paths" (Embedded values) Take this for example:
routes.MapRoute(
"SomePage",
"Net/Fix/{value}",
new { controller = "Net", action = "Index" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I don't know how to explain - I will try.. If I am wrong please explain it
Question 1. Is the first argument in mapRoute so that we can specify which routing we want to take place when using hyperlinks?
Question 2. What does the second argument do? It appears as if the second argument gives you the option of specifying how the routing should occur as below: "Net/Fix/hello" or by specifying placeholders in the form of {controller}/{action}/{somevar}
Question 3: I assume if nothing is used in question 2 scenario - this specifies default routing that should take place?
Question 4: How does ASP.NET infer whether to use a query string or an embedded value..
Because for example when I decide to call my page
http:/localhost:portno/Net/Fix/hello
It dutifully prints hello.. but when I do this
http:/localhost:portno/Net/Index/hello
It doesn't work.. unless I do
http:/localhost:portno/Net/Index?value=hello..
Question is... why?!!!
I hope questions were clear.. I will reply to answers (if any later).
Upvotes: 0
Views: 807
Reputation: 40150
The first argument is a route name. Each route should have a unique name, and they can be used for creating links, to assure a link is based on a certain route. It's not important in your case of matching a route.
The second argument is a matching pattern. Literal values are shown in clear, and parameterized values inside curly braces. {}
. The parameterized values are not just for specifying the location of a parameter, but also the name of it.
I'm not sure offhand why you would define a route without any matching pattern. Does such an overload of MapRoute()
exist?
The reason you get the behavior you do with this url: http:/localhost:portno/Net/Index?value=hello
It matches the second (the default) route, not the first.
However, look at the second route pattern:
"{controller}/{action}/{id}"
The controller is the first parameter, action is the second. So with your URL, that request is routed to the Net
controller, Index
action. the same as your first example.
Because the query string contains a value
parameter, that still gets passed to the action method. And it just so happens your action method has a string
parameter named value
, so it works.
Upvotes: 1