Problem
How do I logically group a large ASP. NET Core 2.0 application?
Answer
Create a new ASP. NET Core 2.0 empty project, modify the startup class, and add MVC services and middleware:
public void Configureservices (Iservicecollection services) {services. Addmvc ();} public void Configure (Iapplicationbuilder app, ihostingenvironment env) {if (env. Isdevelopment ()) {app. Usedeveloperexceptionpage ();} App. USEMVC (Routes =>{routes. MapRoute (Name: "Area", Template: "{area:exists}/{controller=home}/{action=index}/{id}"); Routes. MapRoute (name: "Default", Template: "{controller=home}/{action=index}/{id}");});
Create the directory structure as follows:
Add files to the Controllers and views directory (where the directory structure of AREA1 is as follows):
Add the [area] attribute to the controller class in the AREA1 and AREA2 directories:
namespace areas.areas.area1.controllers{ [Area ("AREA1")] public class Homecontroller:controller { Public Iactionresult Index () { return View (); } Public Iactionresult About () { return View ();}} }
Run, this page displays:
Click the hyperlink area1/home/index, the page displays:
Back to the homepage, click the hyperlink Area2/home/index, page display:
Discuss
MVC uses models, views, and controllers to isolate the application's concerns. For larger applications, the zone provides a way to isolate these three concerns into higher-level groupings. For example, you might want to separate the application into different modules, each containing its own MVC structure.
In order to support routing, there is another routing parameter area (as a complement to the Controller and Action parameters). You can imagine a zone as a controller namespace. In the context of the current request, the area route parameter can also exist as an environment variable (no display provided). In the following example, because the page exists in the Area1 area, hyperlinks to the region omit the area parameter (discussed in detail in the following section):
<p><strong>you is here: </strong>Area1/Home/Index</p><ul> <li><a Asp-area= "" asp-controller= "Home" asp-action= "Index" >Home/Index</a></li> <li><a Asp-area= "asp-controller=" Home "asp-action=" about ">Home/About</a></li> <li><a Asp-controller= "Home" asp-action= "Index" >Area1/Home/Index</a></li> <li><a Asp-controller= "Home" asp-action= "about" >Area1/Home/About</a></li> <li><a asp-area= " Area2 "asp-controller=" Home "asp-action=" Index ">Area2/Home/Index</a></li> <li><a Asp-area= "AREA2" asp-controller= "Home" asp-action= "about" >Area2/Home/About</a></li></ul>
In order to use zones in a project, you first need to create a directory named areas and each specific area containing its controllers, models, and views. It is important to note the naming of the directory structure, because MVC finds the location of the view in the following order:
Once the directory structure is created successfully, you can identify each controller through the [area] feature:
[Area ("Area1")]public class homecontroller:controller{}
Generate links
The following table lists the route parameters required to generate the hyperlink, where from is the page where the hyperlink is located, the page to which the hyperlink needs to jump, and the link format: {area}/{controller}/{action}.
If you omit a route parameter, it means that MVC retrieves the parameters from the context of the current request. For ease of maintenance, I personally find it convenient to set values for each route parameter. The sample project included with this article contains hyperlinks on different pages, and you can see how the environment variable values work:
From |
To |
Route parameters |
Area1/controller1/action1 |
area1/controller1/Action2 |
asp-action= "Action2" |
Area1/controller1/action1 |
area1/Controller2/action1 |
Asp-controller= "Controller2" asp-action= "Action1" |
Area1/controller1/action1 |
Area2/controller1/action1 |
Asp-area= "AREA2" Asp-controller= "Controller1" asp-action= "Action1" |
Area1/controller1/action1 |
Controller1/action1 |
Asp-area= "" Asp-controller= "Controller1" asp-action= "Action1" |
Layout page
The razor view inside the area can use the outside layout page (that is,/views/shared in the root directory). Of course you can define a different layout page for each area. Another way is to define the public layout page externally, and then the layout page within the area as the page layout, creating a nested layout page. The following shows how the layout page of the AREA2 area uses the external layout page (note that in order to reference the external layout page, you need to specify an absolute path):
@{ Layout = "/views/shared/_layout.cshtml";} <div> <nav style= "Background-color:lightgray" >
Source code DownloadOriginal: https://tahirnaushad.com/2017/08/25/asp-net-core-2-0-mvc-areas/
ASP. NET Core 2.0 series Articles DirectoryTranslation ASP. NET Core 2.0 Zone