Use Area and note in mvc4area, mvc4area

Source: Internet
Author: User

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:

1 namespace MvcApp4.Areas. admin 2 {3 public class AdminAreaRegistration: AreaRegistration 4 {5 public override string AreaName 6 {7 get 8 {9 return "Admin "; 10} 11} 12 13 public override void RegisterArea (AreaRegistrationContext context) 14 {15 context. mapRoute (16 "Admin_default", 17 "Admin/{controller}/{action}/{id}", 18 new {controller = "home", action = "Index ", id = UrlParameter. optional}, 19 namespaces: new [] {"MvcApp4.Areas. admin. controllers "} // specify the namespace of the controller class for this route query 20); 21} 22} 23}

 

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

1 protected void Application_Start() {2     AreaRegistration.RegisterAllAreas();3 4     WebApiConfig.Register(GlobalConfiguration.Configuration);5     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);6     RouteConfig.RegisterRoutes(RouteTable.Routes);7     BundleConfig.RegisterBundles(BundleTable.Bundles);8 }

 

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

 

 1     public class HomeController : Controller 2     { 3         // 4         // GET: /Admin/Home/ 5  6         public ActionResult Index() 7         { 8             return Content("hello world"); 9         }10 11     }

 

 

 

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. cs under App_start
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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.