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
- index.html // Home page
Under normal mvc, we need to write every controller and view on a fixed page, so that all files are stacked in these two directories.
For ordinary small projects, this may be a more convenient solution, but the default mvc directory is when the project directory needs to be partitioned carefully.
We can't achieve what we need.
Usually we do this in two ways
1. Custom Routing + Custom View Engine
2. Use area to manage each directory
Examples are as follows:
Description: The system includes three main types of users: students, teachers and administrators. It classifies the relevant functional codes of each user as a whole, facilitating centralized management as well as facilitating centralized management.
Distinguishing other functions to avoid ambiguity in more documents
1. Custom Routing + Custom View Engine
First, we 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 routing rules 6 routes.MapRoute( 7 //According to the convention, the uniqueness of the routing name needs to be guaranteed. 8 name: "teacher", 9 //Here by adding teacher Part to differentiate teachers'functional pages 10 url: "teacher/{controller}/{action}/{id}", 11 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 12 //Here, because multiple routing rules use the name Home Of controller,So need 13 //Enter the 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 rules 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 }
Rewrite View Engine Part
Construct a class that inherits the RazorViewEngine class and overrides the contents 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 rewritten view engine with the global Global.asax file location
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RouteConfig.RegisterRoutes(RouteTable.Routes); //Notice here ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new ViewEngine()); }
According to the online tutorials after trying to find that it can achieve the purpose of customizing the directory, but according to the view here to parse the directory, the system defaults to provide
There is only one way to ensure access to the results of parsing and customized parsing. If there is improper use, it is well known. Here's an inappropriate one
The solution is to store the home page directly in the default location and manage it independently. Then remove the "~/Views/{1}/{0}.cshtml" item and add
"~/Views/Home/{1}/{0}.cshtml"
So far, the first approach has come to an end.
2. Use area to manage each directory
Through the first approach, we can achieve the functions we need, but for more functional projects, each function needs to be handled separately.
This requires rewriting a large number of routing rules and view matching rules, or perhaps not knowing enough about mvc, so no better way is found here.
You can use the area area to manage its directory structure, as follows
Create a regional framework by right-clicking the new area of the project directory, and then create the following directory structure
C:\USERS\IVES\DESKTOP\SCMS\SCMS\AREAS
├─admin
│ ├─Controllers
│ ├─Models
│ └─Views
│ ├─Home
│ └─Shared
├─manager
│ ├─Controllers
│ ├─Models
│ └─Views
│ └─Shared
└─teacher
├─Controllers
├─Models
└─Views
└─Shared
Here we can find that the directory structure tree of each region contains a separate MVC structure. We just need to put the contents of each directory into the corresponding region.
See the figure below for details.
As shown in the figure, we just need to write the relevant content directly under the project path directly in the corresponding area, and there is no difference between the other and the original way of using it.
The second way here is straightforward, simple and fast. It is recommended to use it.
Record it, leave it for later examination, and make it convenient for others.
Contact me for communication. renhanlinbsl@163.com
2017.11.15
15:37