ASP.NET Core Areas

ASP.NET Core Areas表示一个应用程序的功能模块,例如:administration,billing,sales等,如果应用程序非常大,有非常多的控制器、模型类、视图,这时我们可以创建多个Areas并将应用程序文件放到不同Areas下,这将更好帮助我们管理应用程序
Areas有他们自己的MVC目录结构,他们有自己的Controllers、Model、Views文件夹.
1 ASP.NET Core Areas例子

在应用程序中创建一个新的Area,在应用程序的根目录下创建一个Areas文件夹命名为Sales,现在在Areas文件夹内创建Controllers、Modes、Views,如下面例子所示:

ASP.NET Core Areas

2 Areas路由

在应用程序的Pragram.cs类添加一个Area路由,下面代码:

app.MapControllerRoute(    name: "areas",    pattern: "{area:exists}/{controller=Home}/{action=Index}");
area部分匹配url指定areas目标控制器,exists说明路由必须匹配这个约束,这个area路由匹配使用了[Area("Area name")]的所有控制器

3 Sales Area创建Model、View、Controller

为了理解Sales Area如何工作,首先创建一个新类叫Products.cs 在Areas->Sales->Models文件夹下
namespace AspNetCore.Areas.Areas.Sales.Models{    public class Product    {        public string Name { get; set; }        public int Quantity { get; set; }        public int Price { get; set; }    }}

接下来创建一个新的controller在Areas->Sales->Controllers文件夹下, 命名为HomeController.cs,添加下面代码:

using AspNetCore.Areas.Areas.Sales.Models;using Microsoft.AspNetCore.Mvc;namespace AspNetCore.Areas.Areas.Sales.Controllers{    [Area("Sales")]    public class HomeController : Controller    {        public IActionResult Index()        {            Product[] productArray = new Product[] {                new Product { Name = "Pants", Quantity = 5, Price=100 },                new Product { Name = "Shirts", Quantity = 10, Price=80 },                new Product { Name = "Shoes", Quantity = 15, Price=50 }            };            return View(productArray);        }    }}
注意[Area("Sales")]特性使用在控制器上,使用他来关联Sales Area和控制器

ASP.NET Core 帮助标签在Areas不工作, 我们必须在Areas内Razor View Imports文件内注册帮助标签,否则帮助标签不工作,在Areas->Sales->Views文件夹下创建_ViewImports.cshtml并且注册帮助标签

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

最后一部分在Areas->Sales->Views->Home文件夹下添加Index视图,内容如下:

@using AspNetCore.Areas.Areas.Sales.Models@model Product[]@{    Layout = null;}<!DOCTYPE html><html><head>    <title>Routing</title></head><body>    <table>        <tr>            <th>Name</th>            <th>Quantity</th>            <th>Price</th>        </tr>        @foreach (Product p in Model)        {            <tr>                <td>@p.Name</td>                <td>@p.Quantity</td>                <td>@p.Price</td>            </tr>        }    </table></body></html>

这个视图将在HTML表格中显示所有产品,运行应用程序,我们将看到Sales Area的HomeController的Action方法被调用并且产品显示在浏览器上

ASP.NET Core Areas

4 使用路由生成到Areas的连接
asp-action="action_name" 帮助标签创建一个相同area控制器方法的连接
例子:在Sales Area的HomeController的Index方法添加下面代码来创建一个链接
<a asp-action="List">Link1</a>

将会生成如下链接:

<a href="/Sales/Home/List">Link1</a>
相同Area别的控制器下的action方法,我们能使用asp-controller="controller_name"帮助标签
<a asp-action="List" asp-controller="Report">Link</a>

将会生成如下链接:

<a href="/Sales/Report/List">Link</a>
5 asp-area 帮助标签
asp-area帮助标签表示应用程序的area部分,我们在应用程序中创建一个新的area命名为Employee

我们使用相同的步骤-在Areas文件夹下创建一个新的文件夹叫Employee,接下来在Employee文件夹下创建Controllers,Modesl,Views

Area->Employee->Controllers文件夹下添加HomeController使用下面代码:

using Microsoft.AspNetCore.Mvc;namespace AspNetCore.Areas.Areas.Employee.Controllers{    [Area("Employee")]    public class HomeController : Controller    {        public IActionResult Index()        {            return View();        }    }}

修改Areas->Sales->Views->Home下的Index视图,创建一个链接使用asp-area帮助标签

<a asp-action="List" asp-controller="Home" asp-area="Employee">Link3</a>

你将会发现生成如下链接:

<a href="/Employee/Home/List">Link3</a>

6 从Area链接到应用程序主体目录

我们可以在areas控制器通过使用asp-area=""来连接到非area控制器,为了测试,在Sales Area的HomeController的Index试图内添加下面代码:
<a asp-action="List" asp-controller="Home" asp-area="">Link3</a>

将生成下面链接:

<a href="/Home/List">Link4</a>

总结

这节我们讲解了ASP.NET Core中Areas和路由的特性,以及使用它来创建URL

源代码地址

https://github.com/bingbing-gui/Asp.Net-Core-Skill/tree/master/Fundamentals/AspNetCore.Route/AspNetCore.Areas