ASP.NET Core 中文文檔 第四章 MVC(4.6)Areas(區域)


原文:Areas
作者:Dhananjay Kumar 和 Rick Anderson
翻譯:耿曉亮(Blue)
校對:許登洋(Seay)

Areas 是 ASP.NET MVC 用來將相關功能組織成一組單獨命名空間(路由)和文件夾結構(視圖)的功能。使用 Areas 創建層次結構的路由,是通過添加另一個路由參數 area 到 Controller 和 action

Areas 提供了一種把大型 ASP.NET Core MVC Web 應用程序分為較小的功能分組的方法。Area 是應用程序內部一個有效的 MVC 結構。在 MVC 項目中,像 Model,Controller 和 View 的邏輯組件放在不同的文件夾中,MVC 用命名約定來創建這些組件間的關系。對於大型應用,它有利於把應用分割成獨立高級功能的 Areas。例如,一個多業務單元的電子商務應用,如結賬,計費和搜索等。每個單元都有自己的邏輯組件:視圖、控制器和模型。在這種情況下,你可以用 Areas 在同一項目中物理分割業務組件。

在 ASP.NET Core MVC 項目中 Area 被定義成有自己的一套 controller,view 和 model 的較小的功能單元。

當有下列情況時應當考慮在 MVC 項目中用 Areas:

  • 你的應用程序應該從邏輯上分隔成多個高級功能組件的
  • 你想要分隔你的 MVC 項目,使每一個功能 area 可以獨立工作

Area 特性:

  • 一個 ASP.NET Core MVC 應用可以有任意數量的 area
  • 每一個 area 都有自己的控制器、模型和視圖
  • 允許把大型 MVC 項目組織成多個高級組件以便可以獨立工作
  • 支持具有相同名稱的多個控制器 - 只要它們有不同的 areas

讓我們看一個例子,說明如何創建和使用 Areas。比如在一個商店應用程序里有兩個不同分組的控制器和視圖:Products 和 Services。下一個典型的文件夾結構,使用 MVC Area 看起來像下面:

  • Project name
    • Areas
      • Products
        • Controllers
          • HomeController.cs
          • ManageController.cs
        • Views
          • Home
            • Index.cshtml
          • Manage
            • Index.cshtml
      • Services
        • Controllers
          • HomeController.cs
        • Views
          • Home
            • Index.cshtml

當 MVC 嘗試在 Area 中渲染一個視圖時,默認情況下,會嘗試在下面位置中查找:

/Areas/<Area-Name>/Views/<Controller-Name>/<Action-Name>.cshtml
/Areas/<Area-Name>/Views/Shared/<Action-Name>.cshtml
/Views/Shared/<Action-Name>.cshtml

這些默認的位置可以通過Microsoft.AspNetCore.Mvc.Razor.RazorViewEngineOptions 的 AreaViewLocationFormats 方法被修改。

例如,在下面的代碼中文件夾名為 ‘Areas’,它被修改為 ‘Categories’。

services.Configure<RazorViewEngineOptions>(options =>
{
    options.AreaViewLocationFormats.Clear();
    options.AreaViewLocationFormats.Add("/Categories/{2}/Views/{1}/{0}.cshtml");
    options.AreaViewLocationFormats.Add("/Categories/{2}/Views/Shared/{0}.cshtml");
    options.AreaViewLocationFormats.Add("/Views/Shared/{0}.cshtml");
});

需要注意的是 Views 文件夾結構是唯一需要重點考慮的並且剩余文件夾像 Controllers 和 Models 的內容並不重要。比如,根本不需要 Controllers 和 Models 文件夾。這是因為 Controllers 和 Models 的內容只是編譯成一個 .dll 的代碼不是作為 Views 的內容直到 view 被請求。

一旦定義了文件夾層次結構,需要告訴 MVC 每一個相關的 area 的 controller。用 [Area] 特性修飾控制器名稱。

...
namespace MyStore.Areas.Products.Controllers
{
    [Area("Products")]
    public class HomeController : Controller
    {
        // GET: /Products/Home/Index
        public IActionResult Index()
        {
            return View();
        }

        // GET: /Products/Home/Create
        public IActionResult Create()
        {
            return View();
        }
    }
}

用新創建的 areas 設置一個路由的定義。Routing to Controller Actions 詳細介紹了如何創建路由定義, 包括使用傳統路由與特性路由。在本例中,我們會用傳統路由。想這樣做, 只需打開 Startup.cs 文件並通過添加下邊高亮的路由定義修改它。

...
app.UseMvc(routes =>
{
  routes.MapRoute(name: "areaRoute",
    template: "{area:exists}/{controller=Home}/{action=Index}");

  routes.MapRoute(
      name: "default",
      template: "{controller=Home}/{action=Index}");
});

瀏覽 http:// /products , Products area 中 HomeController 的 Index 方法將會被調用。

生成鏈接

  • 從一個基礎 controller 的 area 中的方法生成鏈接到同一 controller 的另一個方法。
    當前請求路徑像 /Products/Home/Create
    HtmlHelper 語法:@Html.ActionLink("Go to Product's Home Page", "Index")
    TagHelper 語法:<a asp-action="Index">Go to Product's Home Page</a>
    注意這里不需要提供 ‘area’ 和 ‘controller’ 值因為他們在當前請求上下文中已經可用。這種值被稱作 ambient 值。

  • 從一個基礎 controller 的 area 中的方法生成鏈接到不同 controller 的另一個方法。
    當前請求路徑像 /Products/Home/Create
    HtmlHelper 語法:@Html.ActionLink("Go to Manage Products’ Home Page", "Index", "Manage")
    TagHelper 語法:<a asp-controller="Manage" asp-action="Index">Go to Manage Products’ Home Page</a>
    注意這里用的 ‘area’ 環境值是上面 ‘controller’ 明確指定的。

  • 從一個基礎 controller 的 area 中的方法生成鏈接到不同 controller 和不同 area 另一個方法。
    當前請求路徑像 /Products/Home/Create
    HtmlHelper 語法:@Html.ActionLink("Go to Services’ Home Page", "Index", "Home", new { area = "Services" })
    TagHelper 語法:<a asp-area="Services" asp-controller="Home" asp-action="Index">Go to Services’ Home Page</a>
    注意這里沒有環境值被用。

  • 從一個基礎 controller 的 area 中的方法生成鏈接到不在一個 area 中的不同 controller 的另一個方法。
    HtmlHelper 語法:@Html.ActionLink("Go to Manage Products’ Home Page", "Index", "Home", new { area = "" })
    TagHelper 語法:<a asp-area="" asp-controller="Manage" asp-action="Index">Go to Manage Products’ Home Page</a>
    因此生成鏈接到非 area 的基礎 controller 方法,清空了這里 ‘area’ 的環境值。

發布 Areas

發布 areas 文件夾的所有 view,在 project.json 包含一個條目在 publishOptions 的 include 節點如下:

"publishOptions": {
"include": [
  "Areas/**/*.cshtml",
  ....
  ....
]

返回目錄


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM