ASP.NET MVC Routing System

I have some questions in MVC routing. I am trying to understand how the MVC routing works, but seems I am lost in the way. Your help in this, is greatly appreciated.

I have defined two routes in Global.asax 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 are (these may be pretty simple, but I am confused)
1) when I type "~/Public/Account/Register" it goes to Account controller and Register action. Why the 1st segment(Public) is not taken as value for controller ?

2)When I type "~/Shop/OldAction it goes to Home controller and Index action. please describe.

3)Does the routing system takes 1st segment as controller and 2nd segment as action defaultly ?

4)Is routing system takes anything we mentioned within { and } in the url pattern as segment variables ?


UPDATE : Please Share your knowledge in this matter. Much needed.

Thanks


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

链接地址: http://www.djcxy.com/p/36760.html

上一篇: 在ASP.NET MVC区域路由中找不到404错误

下一篇: ASP.NET MVC路由系统