Use Area and note in mvc4area, mvc4area
In MVC projects, Area is often used to separate different modules to make the project structure clearer.
The procedure is as follows:
Project-> Add->Region (Area)
Enter Admin
After successful addition
Area includes:
Similar to creating an empty MVC project, Admin Area has its own Controllers, Models, and Views folders. The difference is that there is an additional AdminAreaRegistration. cs file, which defines a class called AdminAreaRegistration. Its content is as follows:
The root directory can have the same structure for front-end development, while the admin directory is generally developed as the administrator background!
AdminAreaRegistration. cs file, which defines a class called AdminAreaRegistration. Its content is as follows:
Namespace MvcApp4.Areas. admin {public class AdminAreaRegistration: AreaRegistration {public override string AreaName {get {return "Admin" ;}} public override void RegisterArea (AreaRegistrationContext context) {context. mapRoute ("Admin_default", "Admin/{controller}/{action}/{id}", new {controller = "home", action = "Index", id = UrlParameter. optional}, namespaces: new [] {"MvcApp4.Areas. admin. controllers "} // specify the namespace of the controller class for this route );}}}
Note that you need to add the namespace of Areas to control the parameters received by controllers. Otherwise, an error will occur during access.
Namespaces: new [] {"MvcApp4.Areas. Admin. Controllers "}
The MapRoute method of the AreaRegistrationContext class is the same as the MapRoute method of App_Start-> RouteConfig. cs. It only distinguishes routing control under the Area directory!
The Application_Start method in Global. asax automatically adds such a code
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); }
Call the AreaRegistration. RegisterAllAreas method to make the MVC application look for all classes inherited from AreaRegistration after startup, and call their RegisterArea method for each of these classes.
Here is a Demo.
Create two new access connections with the same content. Both are simple outputs of a "hello World"
Locate the URL (areas/admin)
Http: // localhost: 18291/Admin/Home/Index
Locate the URL (root directory)
Http: // localhost: 18291/Home/Index
public class HomeController : Controller { // // GET: /Admin/Home/ public ActionResult Index() { return Content("hello world"); } }
If you have not added the following content:
namespaces: new[] { "MvcApp4.Areas.Admin.Controllers" }
The following error occurs after running:
However, if we change the output of/Home/Index in the Root directory to "Root Say hello World", you will find that the output is "hello World ",
This isController Ambiguity
This is another area of attention.
We need to add a namespaces in RouteConfig. cs under App_start to declare the namespace accessed by the Controller!
// RouteConfig under App_start. cs public class RouteConfig {public static void RegisterRoutes (RouteCollection routes) {routes. ignoreRoute ("{resource }. axd/{* pathInfo} "); routes. mapRoute (name: "Default", url: "{controller}/{action}/{id}", defaults: new {controller = "Home", action = "Index ", id = UrlParameter. optional}, namespaces: new [] {"MvcApp4.Controllers"} // specify the namespace controllers of the controller class for this route query);} // \ Admin \ AdminAreaRegistration under areas. cs public class AdminAreaRegistration: AreaRegistration {public override string AreaName {get {return "Admin" ;}} public override void RegisterArea (AreaRegistrationContext context) {context. mapRoute ("Admin_default", "Admin/{controller}/{action}/{id}", new {controller = "home", action = "Index", id = UrlParameter. optional}, namespaces: new [] {"MvcApp4.Areas. admin. controllers "} // the controllers of the corresponding namespace );}}
In this way, you can differentiateController