Asp.net mvc area implements Multi-Level controller and multi-level view, mvcarea

Source: Internet
Author: User

Asp.net mvc area implements Multi-Level controller and multi-level view, mvcarea

It is often necessary to describe such a project structure

~ :. // Web root directory
─ ──. Admin // administrator function directory
│ ──Index.html // Administrator directory page
─ ──. User // user Function directory
│ ──Index.html // User Function directory
Registry.index.html // Home Page

Under normal mvc, We need to write every controller and view on a fixed page so that all the files are stacked in these two directories,

For ordinary small projects, this may be a convenient solution, but when you need to carefully divide the project directory, the default mvc directory is

We cannot implement the functions we need.

We generally use two methods to achieve this.

1. Use custom routing + custom view Engine

2. Use area to manage each directory

Example:

Description: The system consists of three main user types: students, teachers, and administrators. The code classification of related functions of each user is complete to facilitate centralized management.

Differentiate other functions to avoid ambiguity when there are too many files

1. Use custom routing + custom view Engine

First, define the routing rules and configure them in the App_Start/RouteConfig. cs file under the project directory.

1 public static void RegisterRoutes (RouteCollection routes) 2 {3 routes. ignoreRoute ("{resource }. axd/{* pathInfo} "); 4 5 // Add custom route Rule 6 routes. mapRoute (7 // ensure the uniqueness of the route name according to the Conventions. 8 name: "teacher", 9 // Add the teacher section to distinguish the instructor's function page. 10 url: "teacher/{controller}/{action}/{id}", 11 defaults: new {controller = "Home", action = "Index", id = UrlParameter. optional}, 12 // here because multiple routing Rules use a controller named Home, you need 13 // input a namespace to ensure the uniqueness of the controller. 14 namespaces: new string [] {"SCMS. controllers. teacher "}); 15 routes. mapRoute (16 name: "manager", 17 url: "manager/{controller}/{action}/{id}", 18 defaults: new {controller = "Home ", action = "Index", id = UrlParameter. optional}, 19 namespaces: new string [] {"SCMS. controllers. manager "}); 20 routes. mapRoute (21 name: "admin", 22 url: "admin/{controller}/{action}/{id}", 23 defaults: new {controller = "Home ", action = "Index", id = UrlParameter. optional}, 24 namespaces: new string [] {"SCMS. controllers. admin "}); 25 26 // system-defined routing Rule 27 routes. mapRoute (28 name: "Default", 29 url: "{controller}/{action}/{id}", 30 defaults: new {controller = "Home ", action = "Index", id = UrlParameter. optional}, 31 namespaces: new string [] {"SCMS. controllers "}); 32}

Overwrite view engine Section

Construct the following class, inherit the RazorViewEngine class, and override the content of ViewLocationFormats.

public class ViewEngine : RazorViewEngine    {        public ViewEngine()        {            ViewLocationFormats = new[]            {                "~/Views/{1}/{0}.cshtml",                "~/Views/Shared/{0}.cshtml",                "~/Views/admin/{1}/{0}.cshtml",                "~/Views/teacher/{1}/{0}.cshtml",                "~/Views/manager/{1}/{0}.cshtml"            };        }    }

Clear the original view engine and pass in the overwritten view engine. The file is located in the Global. asax file.

Protected void Application_Start () {AreaRegistration. registerAllAreas (); RouteConfig. registerRoutes (RouteTable. routes); // note that ViewEngines. engines. clear (); ViewEngines. engines. add (new ViewEngine ());}

According to the online tutorial, after trying, you can find that you can customize the directory, but according to the view parsing directory here, the system provides

There is only one resolution method and one custom resolution method that can guarantee access results. If you are using this method improperly, please refer to the following. This is not an appropriate example.

The solution is directly stored on the homepage of the default location and managed independently. Then remove "~ /Views/{1}/{0}. cshtml ", add

"~ /Views/Home/{1}/{0}. cshtml"

So far, the first method has ended.

2. Use area to manage each directory

Although the first method can implement the functions we need, for projects with many functions, each function needs to be processed separately,

In this case, a large number of routing rules and view matching rules need to be rewritten, or the mvc is not well understood, so no better solution is found.

You can use the area to manage its directory structure, as shown below:

Right-click the project directory to create a region and create the following directory structure.

C: \ USERS \ IVES \ DESKTOP \ SCMS \ AREAS
├ ── Admin
│ ├ ── Controllers
│ ─ ── Models
│ └ ── Views
│ ├ ── Home
│ Mongo── Shared
Manager
│ ├ ── Controllers
│ ─ ── Models
│ └ ── Views
│ Mongo── Shared
└ ── Teacher
├ ── Controllers
─ ── Models
Parameter-Views
└ ── Shared

Here we can find that the directory structure tree of each region contains an independent MVC structure. We only need to put the content of each directory into the corresponding region.

For details, see

You only need to write the content directly written in the project path in the corresponding area. The other content is no different from the original one.

The second method is both direct mounting and simple and fast. We recommend that you use

 

Record it and keep it for future reference. It is also convenient for others.

Contact me. Renhanlinbsl@163.com

2017.11.15

PM

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.