使用應用程式組件共用控制器、檢視、Razor 頁面等等

作者:Rick Anderson

檢視或下載範例程式碼 \(英文\) (如何下載)

應用程式組件是應用程式資源的抽象概念。 應用程式組件可讓 ASP.NET Core 探索控制器、檢視元件、標籤協助程式、Razor Pages、Razor 編譯來源等等。 AssemblyPart 是應用程式組件。 AssemblyPart 會封裝組件參考,並公開類型和編譯參考。

「功能提供者」會使用應用程式組件,來填入 ASP.NET Core 應用程式的功能。 應用程式組件的主要使用案例是設定應用程式,以探索 (或避免載入) 組件中的 ASP.NET Core 功能。 例如,您可能想要在多個應用程式之間共用一般功能。 您可以使用應用程式組件與多個應用程式共用組件 (DLL),其中包含控制器、檢視、Razor Pages、Razor 編譯來源、標籤協助程式等等。 共用組件最好是在多個專案中複製程式碼。

ASP.NET Core 應用程式會從 ApplicationPart 載入功能。 AssemblyPart 類別代表組件所支援的應用程式組件。

載入 ASP.NET Core 功能

使用 Microsoft.AspNetCore.Mvc.ApplicationPartsAssemblyPart 類別來探索和載入 ASP.NET Core 功能 (控制器、檢視元件等)。 ApplicationPartManager 會追蹤可用的應用程式組件和功能提供者。 ApplicationPartManager 是在 Startup.ConfigureServices 中設定:

// Requires using System.Reflection;
public void ConfigureServices(IServiceCollection services)
{
    var assembly = typeof(MySharedController).Assembly;
    services.AddControllersWithViews()
        .AddApplicationPart(assembly)
        .AddRazorRuntimeCompilation();

    services.Configure<MvcRazorRuntimeCompilationOptions>(options => 
    { options.FileProviders.Add(new EmbeddedFileProvider(assembly)); });
}

下列程式碼提供使用 AssemblyPart 設定 ApplicationPartManager 的替代方法:

// Requires using System.Reflection;
// Requires using Microsoft.AspNetCore.Mvc.ApplicationParts;
public void ConfigureServices(IServiceCollection services)
{
    var assembly = typeof(MySharedController).Assembly;
    // This creates an AssemblyPart, but does not create any related parts for items such as views.
    var part = new AssemblyPart(assembly);
    services.AddControllersWithViews()
        .ConfigureApplicationPartManager(apm => apm.ApplicationParts.Add(part));
}

上述兩個程式碼範例會從組件載入 SharedControllerSharedController 不在應用程式的專案中。 請參閱 WebAppParts 解決方案範例下載。

包含檢視

使用 Razor 類別庫在組件中包含檢視。

防止載入資源

應用程式組件可用來避免在特定組件或位置中載入資源。 新增或移除 Microsoft.AspNetCore.Mvc.ApplicationParts 集合的成員,以隱藏或提供可用的資源。 ApplicationParts 集合中的項目順序並不重要。 設定 ApplicationPartManager 之後,再用它來設定容器中的服務。 例如,設定 ApplicationPartManager 之後,再叫用 AddControllersAsServices。 在 ApplicationParts 集合上呼叫 Remove 以移除資源。

ApplicationPartManager 包含下列項目的組件:

  • 應用程式的組件和相依組件。
  • Microsoft.AspNetCore.Mvc.ApplicationParts.CompiledRazorAssemblyPart
  • Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
  • Microsoft.AspNetCore.Mvc.TagHelpers.
  • Microsoft.AspNetCore.Mvc.Razor.

功能提供者

應用程式功能提供者會檢查應用程式組件,並對這些組件提供功能。 下列 ASP.NET Core 功能均內建功能提供者:

繼承自 IApplicationFeatureProvider<TFeature> 的功能提供者,其中 T 是功能的類型。 功能提供者可以針對任何先前列出的功能類型實作。 ApplicationPartManager.FeatureProviders 中功能提供者的順序可能會影響執行階段行為。 稍後新增的提供者可以回應先前新增提供者所採取的動作。

顯示可用的功能

應用程式可用的功能,可透過相依性插入要求 ApplicationPartManager 來列舉:

using AppPartsSample.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Controllers;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.AspNetCore.Mvc.Razor.TagHelpers;
using Microsoft.AspNetCore.Mvc.ViewComponents;

namespace AppPartsSample.Controllers
{
    public class FeaturesController : Controller
    {
        private readonly ApplicationPartManager _partManager;

        public FeaturesController(ApplicationPartManager partManager)
        {
            _partManager = partManager;
        }

        public IActionResult Index()
        {
            var viewModel = new FeaturesViewModel();

            var controllerFeature = new ControllerFeature();
            _partManager.PopulateFeature(controllerFeature);
            viewModel.Controllers = controllerFeature.Controllers.ToList();

            var tagHelperFeature = new TagHelperFeature();
            _partManager.PopulateFeature(tagHelperFeature);
            viewModel.TagHelpers = tagHelperFeature.TagHelpers.ToList();

            var viewComponentFeature = new ViewComponentFeature();
            _partManager.PopulateFeature(viewComponentFeature);
            viewModel.ViewComponents = viewComponentFeature.ViewComponents.ToList();

            return View(viewModel);
        }
    }
}

下載範例會使用上述程式碼來顯示應用程式功能:

Controllers:
    - FeaturesController
    - HomeController
    - HelloController
    - GenericController`1
    - GenericController`1
Tag Helpers:
    - PrerenderTagHelper
    - AnchorTagHelper
    - CacheTagHelper
    - DistributedCacheTagHelper
    - EnvironmentTagHelper
    - Additional Tag Helpers omitted for brevity.
View Components:
    - SampleViewComponent

探索應用程式組件

使用應用程式組件進行開發時,HTTP 404 錯誤不足為奇。 這些錯誤通常是因為缺少應用程式組件探索方式的必要需求所造成。 如果您的應用程式傳回 HTTP 404 錯誤,請確認已符合下列需求:

  • applicationName 設定必須設定為用於探索的根組件。 用於探索的根組件通常是進入點組件。
  • 根組件必須具有用於探索之組件的參考。 參考可以是直接或轉移。
  • 根組件需要參考 Web SDK。 架構具有邏輯,會將屬性戳記到用於探索的根組件中。

作者:Rick Anderson

檢視或下載範例程式碼 \(英文\) (如何下載)

應用程式組件是應用程式資源的抽象概念。 應用程式組件可讓 ASP.NET Core 探索控制器、檢視元件、標籤協助程式、Razor Pages、Razor 編譯來源等等。 AssemblyPart 是應用程式組件。 AssemblyPart 會封裝組件參考,並公開類型和編譯參考。

「功能提供者」會使用應用程式組件,來填入 ASP.NET Core 應用程式的功能。 應用程式組件的主要使用案例是設定應用程式,以探索 (或避免載入) 組件中的 ASP.NET Core 功能。 例如,您可能想要在多個應用程式之間共用一般功能。 您可以使用應用程式組件與多個應用程式共用組件 (DLL),其中包含控制器、檢視、Razor Pages、Razor 編譯來源、標籤協助程式等等。 共用組件最好是在多個專案中複製程式碼。

ASP.NET Core 應用程式會從 ApplicationPart 載入功能。 AssemblyPart 類別代表組件所支援的應用程式組件。

載入 ASP.NET Core 功能

使用 ApplicationPartAssemblyPart 類別來探索和載入 ASP.NET Core 功能 (控制器、檢視元件等)。 ApplicationPartManager 會追蹤可用的應用程式組件和功能提供者。 ApplicationPartManager 是在 Startup.ConfigureServices 中設定:

public void ConfigureServices(IServiceCollection services)
{
    // Requires using System.Reflection;
    var assembly = typeof(MySharedController).GetTypeInfo().Assembly;
    services.AddMvc()
        .AddApplicationPart(assembly)
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

下列程式碼提供使用 AssemblyPart 設定 ApplicationPartManager 的替代方法:

public void ConfigureServices(IServiceCollection services)
{
    // Requires using System.Reflection;
    // Requires using Microsoft.AspNetCore.Mvc.ApplicationParts;
    var assembly = typeof(MySharedController).GetTypeInfo().Assembly;
    var part = new AssemblyPart(assembly);
    services.AddMvc()
        .ConfigureApplicationPartManager(apm => apm.ApplicationParts.Add(part))
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

上述兩個程式碼範例會從組件載入 SharedControllerSharedController 不在應用程式的專案中。 請參閱 WebAppParts 解決方案範例下載。

包含檢視

使用 Razor 類別庫在組件中包含檢視。

防止載入資源

應用程式組件可用來避免在特定組件或位置中載入資源。 新增或移除 Microsoft.AspNetCore.Mvc.ApplicationParts 集合的成員,以隱藏或提供可用的資源。 ApplicationParts 集合中的項目順序並不重要。 設定 ApplicationPartManager 之後,再用它來設定容器中的服務。 例如,設定 ApplicationPartManager 之後,再叫用 AddControllersAsServices。 在 ApplicationParts 集合上呼叫 Remove 以移除資源。

下列程式碼會使用 Microsoft.AspNetCore.Mvc.ApplicationParts 從應用程式移除 MyDependentLibrary

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
            .ConfigureApplicationPartManager(apm =>
            {
                var dependentLibrary = apm.ApplicationParts
                    .FirstOrDefault(part => part.Name == "MyDependentLibrary");

                if (dependentLibrary != null)
                {
                    apm.ApplicationParts.Remove(dependentLibrary);
                }
            });
}

ApplicationPartManager 包含下列項目的組件:

  • 應用程式的組件和相依組件。
  • Microsoft.AspNetCore.Mvc.TagHelpers.
  • Microsoft.AspNetCore.Mvc.Razor.

應用程式功能提供者

應用程式功能提供者會檢查應用程式組件,並對這些組件提供功能。 下列 ASP.NET Core 功能均內建功能提供者:

繼承自 IApplicationFeatureProvider<TFeature> 的功能提供者,其中 T 是功能的類型。 功能提供者可以針對任何先前列出的功能類型實作。 ApplicationPartManager.FeatureProviders 中功能提供者的順序可能會影響執行階段行為。 稍後新增的提供者可以回應先前新增提供者所採取的動作。

顯示可用的功能

應用程式可用的功能,可透過相依性插入要求 ApplicationPartManager 來列舉:

using AppPartsSample.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Controllers;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.AspNetCore.Mvc.Razor.TagHelpers;
using Microsoft.AspNetCore.Mvc.ViewComponents;

namespace AppPartsSample.Controllers
{
    public class FeaturesController : Controller
    {
        private readonly ApplicationPartManager _partManager;

        public FeaturesController(ApplicationPartManager partManager)
        {
            _partManager = partManager;
        }

        public IActionResult Index()
        {
            var viewModel = new FeaturesViewModel();

            var controllerFeature = new ControllerFeature();
            _partManager.PopulateFeature(controllerFeature);
            viewModel.Controllers = controllerFeature.Controllers.ToList();

            var tagHelperFeature = new TagHelperFeature();
            _partManager.PopulateFeature(tagHelperFeature);
            viewModel.TagHelpers = tagHelperFeature.TagHelpers.ToList();

            var viewComponentFeature = new ViewComponentFeature();
            _partManager.PopulateFeature(viewComponentFeature);
            viewModel.ViewComponents = viewComponentFeature.ViewComponents.ToList();

            return View(viewModel);
        }
    }
}

下載範例會使用上述程式碼來顯示應用程式功能:

Controllers:
    - FeaturesController
    - HomeController
    - HelloController
    - GenericController`1
    - GenericController`1
Tag Helpers:
    - PrerenderTagHelper
    - AnchorTagHelper
    - CacheTagHelper
    - DistributedCacheTagHelper
    - EnvironmentTagHelper
    - Additional Tag Helpers omitted for brevity.
View Components:
    - SampleViewComponent

探索應用程式組件

使用應用程式組件進行開發時,HTTP 404 錯誤不足為奇。 這些錯誤通常是因為缺少應用程式組件探索方式的必要需求所造成。 如果您的應用程式傳回 HTTP 404 錯誤,請確認已符合下列需求:

  • applicationName 設定必須設定為用於探索的根組件。 用於探索的根組件通常是進入點組件。
  • 根組件必須具有用於探索之組件的參考。 參考可以是直接或轉移。
  • 根組件需要參考 Web SDK。
    • ASP.NET Core 架構具有自訂建置邏輯,會將屬性戳記到用於探索的根組件中。