Knowledge points used in MVC project development (ASP. NET mvc3.0 Areas)

Source: Internet
Author: User

Preface

We know that the responsibilities of the MVC project are clear, compared with ASP. for net webform, the business logic of the MVC project is well separated from the page display. This method has many advantages, such as testability and scalability. However, in actual development, the number of controller controllers increases as the project scale expands. If there are more than two-digit controllers under the controllers folder, even if you adopt good naming rules or distinguish controllers with different functions in the form of subfolders, the project's readability and maintainability will still be affected. Therefore, in some scenarios, it is very useful to separate files related to a function from an independent project. ASP. net mvc provides the areas (region) concept to achieve this goal.

Web applications usually have two sections: foreground (for users) and background (for administrators). We hope that the URLs starting with/locahost/admin are the backend management addresses,

 
Routes. maproute (// default route "default", // route name "{controller}/{action}/{ID }", // URL with parameters new {controller = "home", Action = "Index", id = urlparameter. optional} // parameter defaults );

This is the default route. We hope that the backend can access it through the URL here, for example, localhost/admin/{controller}/{action}. Then we may need to add such a route

Routes. maproute (// admin route "admin", // route name "admin/{controller}/{action}/{ID }", // URL with parameters new {controller = "Index", Action = "Index", id = urlparameter. optional} // parameter defaults );

But how can we use it in projects ......

 

Question

The first step is to add a region in the web project, which is the focus of this record.

We name the region Admin. Click OK to generate several files and folders. Of course, areas is the root folder.

Open the adminarearegistration. CS file.

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 {Action = "Index", id = urlparameter. optional });}}

The system automatically adds a route for it, which is exactly what we want. Therefore, we also add the admincontroller for testing and View index.

Run F5 directly, and then access it through URL

Now let's add another homecontroller controller and index view and run it again.

 

In this case, you need to modify adminarearegistration. CS and Global. asax to add namespace restrictions to the routes:

/Areas/admin/adminarearegistration. CS

Public override void registerarea (arearegistrationcontext context) {context. maproute ("admin_default", "admin/{controller}/{action}/{ID}", new {Action = "Index", id = urlparameter. optional}, new string [] {"mvcarea. areas. admin. controllers "});}

/Global. asax. CS

 
Public static void registerroutes (routecollection routes) {routes. ignoreroute ("{resource }. axd/{* pathinfo} "); routes. maproute ("default", // route name "{controller}/{action}/{ID}", // URL with parameters new {controller = "home ", action = "Index", id = urlparameter. optional}, // default value of the parameter New String [] {"mvcarea. controllers "});}

Summary

In this way, we can put all the controller and view files related to background management under/areas/admin, and so on, and add such files as members, blogs ), forum (Forum) and multiple areas. Each part has its own top-level folder, and physical files are separated to facilitate management.

This method has been greatly improved, but all files are stored in the same project. When the project scale is large, a better development method is to separate different functional modules into different projects as needed, and finally integrate them into a whole. In this way, each project can be independently developed, tested, and released. As for further optimization, I will take the time to continue making up.

 

Sample Code

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.