MVC itself advocates separation of concerns. But when the business logic of the project itself is complex enough, if all the business logic is written under the Controller folder, you will see a very large variety of named controllers, this time the role of the region is very obvious, the different modules through the region to distinguish, simple and clear.
To add a zone:
The new MVC project is no region. Click on the Add zone, right side of the project, enter a region name.. NET automatically adds the "Areas" folder under the project and creates a folder for the corresponding zone under that folder. Create a "test" area, add the controller "default" under the zone, and change the "default" controller content to:
[Routearea ("test")] [Routeprefix ("default")] Public class Default1controller:controller { [Route ("index")] public ActionResult Index () { return View (); } }
To register a route:
The MVC project creates regrets for automatically generating file "global.asax" files. Locate the Routeconfig.registerroutes (routetable.routes) in the Application_Start function. Enter RegisterRoutes, where you add code Routes. Mapmvcattributeroutes ()
Public classRouteconfig { Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. Mapmvcattributeroutes ();//Registering attribute Routesroutes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional}); } }
Delete arearegistration:
When you create a new area, the project automatically creates arearegistration under the zone. For example, when the default controller is created, "TestAreaRegistration.cs" is automatically generated under the folder in the test area, which is used to register the route in the previous version of MVC5. In the MVC5 because the attribute route is registered in the Global.asax, the file can be deleted directly, otherwise there will be no routing errors found. The content is roughly:
Public classtestarearegistration:arearegistration { Public Override stringAreaName {Get { return "Test"; } } Public Override voidRegisterarea (AreaRegistrationContext context) {context. MapRoute ("Test_default", "Test/{controller}/{action}/{id}", New{action ="Index", id =urlparameter.optional}); } }
The controller in the areas can then be accessed directly through a routed approach:
<class= "btn btn-default" href= "/test1/default/index" >» </ a >
Areas in the "MVC5" (areas)