When it comes to MVC's large web sites, we usually cut into multiple modules. But because in MVC if the controller name is the same, it will go wrong, how to solve this problem? Microsoft is too human, see below.
1. New Area
1) Add Zone
2) Set the zone name
3) Final Display
2. Priority of the default route and zone routes.
We're opening the Global.asax, as shown below
<span style= "FONT-SIZE:18PX;" > Public class MvcApplication:System.Web.HttpApplication { protected void Application_Start () { //Register all zone routes Arearegistration.registerallareas (); Webapiconfig.register (globalconfiguration.configuration); Filterconfig.registerglobalfilters (globalfilters.filters); Register Routing routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (bundletable.bundles); } } </span>
The first line is our regional route registration into Routetable.routes, which is called "First registration wins first", so we have defined a URL route in the region's OrderAreaRegistration.cs. Corresponding if the two registration mechanism exchanges the order of the painting, it will be compared to the default route.
<span style= "FONT-SIZE:18PX;" > public class orderarearegistration:arearegistration {public override string AreaName { // Address file name of the corresponding route get { return "Order"; } } public override void Registerarea (AreaRegistrationContext context) { //route alignment Rule context. MapRoute ( "Order_default", "Order/{controller}/{action}/{id}", new {action = "Index", id = Urlparameter.optional} ); } } </span>
3. Even if the zone is used, there may still be a name conflict
For example, in the two routing mechanisms have the same name, sometimes there may be conflicts, the solution is as follows, to the specified route into the corresponding namespace. When namespaces are specified, MVC will no longer search the entire site category for the controller, but instead use the specified namespace to find it.
<span style= "FONT-SIZE:18PX;" >public override void Registerarea (AreaRegistrationContext context) { //route alignment Rule context. MapRoute ( "Order_default", "Order/{controller}/{action}/{id}", new {action = "Index", id = Urlparameter.optional}, //Specifies the appropriate namespace, so when looking for a route, there is no conflict namespaces:new String[1] {" MvcApplication1.Areas.Order "} ); </span>
4. Advanced features of routing
When we are doing development, how can we pull out the controller separately to manage it? What do you mean? Let the controller in the MVC framework come from a single class to manage, so it's easy to manage. So let's look at the mechanism of view in MVC.
1. View mechanism
Analysis: We already know that MVC's program entry is the Application_Start method under the Global.asax class, where all the routing mechanisms are registered first, so when the zone route and the default route are registered, the HTTP request is sent over to the pair. At this point will go to the assembly is the bin file to find the name of the controller end, and inherit the controller class files, found will take the routing rules in one comparison. So with this feature, we can build a class library on its own, and then build the corresponding class with the controller, just let the program eventually compile into the MVC assembly, so we can find our controller.
MVC's area-related technologies