共用方式為


將功能推出至 ASP.NET Core 應用程式中的目標物件

在本指南中,您將使用目標篩選將功能推出至 ASP.NET Core 應用程式的目標物件。 如需目標篩選的詳細資訊,請參閱向目標受眾推出功能

必要條件

使用功能旗標建立 Web 應用程式

在本節中,您會建立 Web 應用程式,讓用戶能夠登入並使用您之前建立的 Beta 功能旗標。

  1. 使用下列命令建立 Web 應用程式,以針對本地資料庫進行驗證。

    dotnet new webapp --auth Individual -o TestFeatureFlags
    
  2. 流覽至新建立 的 TestFeatureFlags 目錄,並新增下列 NuGet 套件的參考。

    dotnet add package Microsoft.Azure.AppConfiguration.AspNetCore
    dotnet add package Microsoft.FeatureManagement.AspNetCore
    dotnet add package Azure.Identity
    
  3. 執行下列命令,為應用程式建立用戶密碼。

    命令會使用秘密管理員來儲存名為 Endpoints:AppConfiguration的秘密,其會儲存您 應用程式組態 存放區的端點。 將<your-App-Configuration-endpoint>佔位元取代為您的 應用程式組態 市集端點。 您可以在 Azure 入口網站 中找到 應用程式組態 存放區 [概觀] 刀鋒視窗中的端點。

    dotnet user-secrets init
    dotnet user-secrets set Endpoints:AppConfiguration "<your-App-Configuration-endpoint>"
    
  4. 將 Azure 應用程式組態和功能管理新增至您的應用程式。

    1. 您可以使用 DefaultAzureCredential 來向 應用程式組態 存放區進行驗證。 請遵循指示,將認證指派給 應用程式組態 數據讀取者角色。 在執行應用程式之前,請務必允許足夠的時間來傳播許可權。

    2. 使用下列程式碼來更新 Program.cs 檔案。

      // Existing code in Program.cs
      // ... ...
      
      using Azure.Identity;
      
      var builder = WebApplication.CreateBuilder(args);
      
      // Retrieve the endpoint
      string endpoint = builder.Configuration.GetValue<string>("Endpoints:AppConfiguration") 
          ?? throw new InvalidOperationException("The setting `Endpoints:AppConfiguration` was not found.");
      
      // Connect to Azure App Configuration and load all feature flags with no label
      builder.Configuration.AddAzureAppConfiguration(options =>
      {
          options.Connect(new Uri(endpoint), new DefaultAzureCredential())
                 .UseFeatureFlags();
      });
      
      // Add Azure App Configuration middleware to the container of services
      builder.Services.AddAzureAppConfiguration();
      
      // Add feature management to the container of services
      builder.Services.AddFeatureManagement();
      
      // The rest of existing code in Program.cs
      // ... ...
      
  5. 使用應用程式組態中介軟體,從 Azure 應用程式組態啟用設定和功能旗標重新整理。

    使用下列程式碼更新 Program.cs。

    // Existing code in Program.cs
    // ... ...
    
    var app = builder.Build();
    
    // Use Azure App Configuration middleware for dynamic configuration refresh
    app.UseAzureAppConfiguration();
    
    // The rest of existing code in Program.cs
    // ... ...
    
  6. 在 Pages 目錄之下新增名為 Beta 的空白新 Razor 頁面。 包含 Beta.cshtmlBeta.cshtml.cs 這兩個檔案。

    @page
    @model TestFeatureFlags.Pages.BetaModel
    @{
        ViewData["Title"] = "Beta Page";
    }
    
    <h1>This is the beta website.</h1>
    
  7. 開啟 Beta.cshtml.cs,並將 屬性新增 FeatureGateBetaModel 類別。

    using Microsoft.AspNetCore.Mvc.RazorPages;
    using Microsoft.FeatureManagement.Mvc;
    
    namespace TestFeatureFlags.Pages
    {
        [FeatureGate("Beta")]
        public class BetaModel : PageModel
        {
            public void OnGet()
            {
            }
        }
    }
    
  8. 開啟 Pages/_ViewImports.cshtml,使用 @addTagHelper 指令註冊功能管理員標籤協助程式。

    @addTagHelper *, Microsoft.FeatureManagement.AspNetCore
    
  9. 開啟頁面/共用目錄中的 _Layout.cshtml。 在 [首頁] 與 [隱私權] 導覽列項目之間插入新的 <feature> 標籤,如下列醒目提示的這幾行。

    <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
        <div class="container">
            <a class="navbar-brand" asp-area="" asp-page="/Index">TestAppConfigNet3</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                    aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
                <ul class="navbar-nav flex-grow-1">
                    <li class="nav-item">
                        <a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
                    </li>
                    <feature name="Beta">
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-page="/Beta">Beta</a>
                        </li>
                    </feature>
                    <li class="nav-item">
                        <a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
    

啟用 Web 應用程式的目標鎖定

使用目標進行功能評估時,需要有目標內容。 您可以明確提供它做為 API 的參數 featureManager.IsEnabledAsync 。 在 ASP.NET Core 中,也可以透過服務集合提供目標內容做為環境內容,方法是實 作 ITargetingContextAccessor 介面。

目標內容存取子

若要提供目標內容,請將的實作 ITargetingContextAccessor 類型傳遞至 WithTargeting<T> 方法。 如果未提供類型,則會使用預設實作,如下列代碼段所示。 默認目標內容存取子會HttpContext.User.Identity.Name針對 使用 作為 UserIdHttpContext.User.Claims 型別RoleGroups。 如果需要自定義, 您可以參考 DefaultHttpTargetingContextAccessor 來實作您自己的 。 若要深入瞭解如何實作 ITargetingContextAccessor,請參閱 目標功能參考。

// Existing code in Program.cs
// ... ...

// Add feature management to the container of services
builder.Services.AddFeatureManagement()
                .WithTargeting();

// The rest of existing code in Program.cs
// ... ...

注意

針對 Blazor 應用程式,請參閱 將功能管理啟用為範圍服務的指示

作用中的目標篩選

  1. 建置並執行應用程式。 一開始,Beta 項目不會出現在工具列上,因為 [預設百分比] 選項設定為 0。

    使用者未登入且未顯示 Beta 項目

  2. 選取右上角的 [註冊] 連結,以建立新的使用者帳戶。 使用 test@contoso.com 的電子郵件地址。 在 [註冊確認] 畫面上,選取 [按一下這裡以確認您的帳戶]

  3. 使用您在註冊帳戶時設定的密碼,以 test@contoso.com 身分登入。

    Beta 項目現在會出現在工具列上,因為將 test@contoso.com 指定為目標使用者。

    使用者登入並顯示 Beta 項目

    現在使用您在註冊帳戶時設定的密碼,以 testuser@contoso.com 身分登入。 Beta 項目不會出現在工具列上,因為將 testuser@contoso.com 指定為排除的使用者。

下一步

若要深入瞭解功能篩選器,請繼續進行下列檔。

如需 .NET 功能管理程式庫的完整功能概要,請繼續進行下列文件。