ASP. NET MVC Route (v) preface
The preceding pages explain the route system in MVC, which is just about an implementation process that makes it clearer what the routing system does in MVC and where it is located, with vague conceptual descriptions, mind maps, and no way to see the actual use of routing, so here's a brief example of a demo.
Definition of a routed namespace
For routes where URL rules, default values, URL parameter constraints are available on the Web, this article speaks about the definition of namespaces in routing.
We all know the role of routing, to match the request to a reasonable controller name, and to the Controller factory to generate the controller to run the request. However, it is unavoidable to name the same controller in the project, so let's simulate what we will find.
Environment Configuration
In the MVC project, on the controllers file, right-click Join-a controller, named HomeController, and choose an empty template. And then you don't have to control the controller, don't do anything, add the following code outside the namespace where the type is located:
1 namespaceAAAAA2 {3 Public classHomecontroller:controller4 {5 Publicactionresult Index ()6 {7 This. Viewbag.name = This. GetType (). FullName;8 returnView ();9 }Ten } One } A namespacebbbbb - { - Public classHomecontroller:controller the { - Publicactionresult Index () - { - This. Viewbag.name = This. GetType (). FullName; + returnView (); - } + } A}
At this point, the default route definition in the Global.asax file is when the simulation project is referencing an increasing number of assemblies as well as a variety of projects, possibly with repeated naming, at this time:
1 routes. MapRoute (2 "Default",//Route name3 "{Controller}/{action}/{id}",//URL with a number of parameters4 New{controller ="Home", action ="Index", id = urlparameter.optional}//default value of parameters
We also do not have to control, directly start debugging, you will see the following error,
This situation is not willing to happen to anyone, assuming we just need to aaaaa the home controller under the namespace what to do? Good, with the road configuration:
1 routes. MapRoute (2 "",3 "{controller}/{action}",4 New{controller ="Home", action ="Index" },5 NULL,6 New string[] {"AAAAA" }7);
Just have to pass in the required namespace definition in the Maproute method to an array, and then right-AAAAA the index () method in the HomeController type under the namespace, click Join View, After completion in the project's views directory under the home directory has a index.cshtml file is added to the view file, open and add such as the following code:
1 < H2 >@ViewBag. Name</h2>
Now that we run the MVC project again,
At this point some friends may ask, assuming that the system now AAAAA under the namespace search hypothesis is not bbbbb under the search controller, and then give way by this definition:
1 routes. MapRoute (2 "",3 "{controller}/{action}",4 New {controller = "Home", action = "Index"},5 null,6 new string[ ] {"AAAAA", "bbbbb"}7 );
Tell your friends This is not possible, assuming that the AAAAA under the home controller is OK, assuming that the system will continue to search for all types under the BBBBB namespace, the detailed implementation here will be described in the following default controller factory space.
Suppose that you want to search only under the specified namespace as defined:
1 route route= routes. MapRoute ( 2 3 " {controller}/{action} ", 4 new {controller =" Home ", action =" Index "}, 5 null, 6 new string[] {"AAAAA"} 7 ); 8 route. datatokens["Usenamespacefallback"] = false;
The Maproute () method here returns a route object, which we also said earlier that the route object is simply not necessary to get a reference to the route object, which we need to obtain here, Also, set the value of the Usenamespacefallback key in the Datatokens property set to False. This value will be passed to the Controller factory, perhaps in length.
This result is the assumption that no search to the specified controller system will also stop the search.
Jinyuan
Source: http://blog.csdn.net/jinyuan0829
This article is copyrighted by the author and Csdn co-owned, welcome reprint, but without the author's permission must retain this paragraph, and on the article page