Area is similar to the namespace concept in route. It can separate multiple sets composed of controller, view, and route, which is useful in relatively large projects.
1. Create an area
Create an MVC project and right-click "add area" on the solution ".
After adding the project, you will find that there is a top-level directory area in the solution. Below is a small MVC project.
2. register an area
In the application_start () method, arearegistration. registerallareas ();
3. Enter area/controller/action in the address bar to access the Controller and other content in area.
4. By default, you need to add a namespace in route registration to prevent mixed controller names.
View code
1 Public Static Void Registerroutes (routecollection routes ){ 2 Routes. ignoreroute (" {Resource}. axd/{* pathinfo} " ); 3 Routes. maproute ( 4 " Default " , // Route name 5 " {Controller}/{action}/{ID} " ,// URL with Parameters 6 New {Controller = " Home " , Action = " Index " , Id = Urlparameter. optional} 7 ); 8 }
View code
1 Public Static Void Registerroutes (routecollection routes ){ 2 Routes. ignoreroute ( " {Resource}. axd/{* pathinfo} " ); 3 Routes. maproute ( 4 " Default " ,// Route name 5 " {Controller}/{action}/{ID} " , // URL with Parameters 6 New {Controller = " Home " , Action = " Index " , Id =Urlparameter. optional }, 7 New [] { " Workingwithareas. Controllers " } 8 ); 9 }
5. By default, you do not need to change anything. The @ html. actionlink () method will automatically detect your current area and connect to and navigate to the area. But if you need to connect to another area, you need to write the following method:
View code
1 3812@ Html. actionlink ("Click me to go to another area","Index",New{Area ="Support"})
Note: The value of area must be a valid value.