0
votes

I am trying to understand how MVC routing works, but it seems I got lost along the way.

I have defined two routes in the Global.asax file as follows

routes.MapRoute("Public", "Public/{controller}/{action}", new { controller = "Home", action = "Index" }); 
routes.MapRoute("ShopSchema2", "Shop/OldAction", new { controller = "Home", action = "Index" });

My questions—which may be pretty simple:

  1. When I type "~/Public/Account/Register", it goes to the Account controller with the Registeraction. Why the 1st segment(Public) is not taken as value for controller?
  2. When I type "~/Shop/OldAction", it goes to the Home controller with the Index action. please describe.
  3. Does the routing system takes the first segment as controller and the second segment as action by default?
  4. Does the routing system take anything we mention within { and } in the URL pattern as segment variables.
1

1 Answers

0
votes

you have put the "Public" as a fixed phrase in the code below:

Public/{controller}/{action}"

normally there is no "Public" in there. If you want to make public dynamic, you have to put it in parentheses like {public} and add functionality in the following code for "Public":

{ controller = "Home", action = "Index" });    
{ controller = "Home", action = "Index", public="None" });

in the second line you are passing a variable called "Public" to the actionresult which by default carries a string value of "None" and that may be changed based on what you replace public with.

Yet again, it is not common to put any variable before controller variable.

anything in { } works as a variable which takes a default value and can be changes based on the address users enters.

routes.MapRoute("ShopSchema2", "Shop/OldAction", new { controller = "Home", action = "Index" });

above code by default goes to home/index. you have not set any variable in there.

I hope this answers all 3 questions