Skip to content

Commit edca190

Browse files
add tutorial about how to add Beta page for MVC app
1 parent 6e884c2 commit edca190

File tree

1 file changed

+117
-19
lines changed

1 file changed

+117
-19
lines changed

articles/azure-app-configuration/howto-targetingfilter-aspnet-core.md

Lines changed: 117 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ titleSuffix: Azure App Configuration
44
description: Learn how to enable staged rollout of features for targeted audiences.
55
ms.service: azure-app-configuration
66
ms.devlang: csharp
7-
author: maud-lv
8-
ms.author: malev
9-
ms.topic: conceptual
10-
ms.date: 02/16/2024
7+
author: zhiyuanliang
8+
ms.author: zhiyuanliang
9+
ms.topic: how-to
10+
ms.date: 03/05/2024
1111
---
1212

1313
# Enable staged rollout of features for targeted audiences
1414

15-
Feature flags allow you to dynamically activate or deactivate functionality in your application. Feature filters determine the state of a feature flag each time it's evaluated. The `Microsoft.FeatureManagement` library includes `TargetingFilter`, which enables a feature flag for a specified list of users and groups, or for a specified percentage of users. `TargetingFilter` is "sticky." This means that once an individual user receives a feature, they'll continue to see that feature on all future requests. You can use `TargetingFilter` to enable a feature for a specific account during a demo, to progressively roll out new features to users in different groups or "rings," and much more. For more information, see [Targeting](https://github.com/microsoft/FeatureManagement-Dotnet#targeting).
15+
Targeting is a feature management strategy that enables developers to progressively roll out new features to their user base. The strategy is built on the concept of targeting a set of users known as the target audience. An audience is made up of specific users, groups, and a designated percentage of the entire user base.
16+
17+
- The users can be actual user accounts, but they can also be machines, devices, or any uniquely identifiable entities to which you want to roll out a feature.
18+
19+
- The groups are up to your application to define. They can be Microsoft Entra groups, your workloads at different locations, or any common attributes based on which you want to categorize your audience.
1620

1721
In this article, you learn how to roll out a new feature in an ASP.NET Core web application to specified users and groups, using `TargetingFilter` with Azure App Configuration.
1822

@@ -21,23 +25,125 @@ In this article, you learn how to roll out a new feature in an ASP.NET Core web
2125
- Finish the [Quickstart: Add feature flags to an ASP.NET Core app](./quickstart-feature-flag-aspnet-core.md).
2226
- Update the [`Microsoft.FeatureManagement.AspNetCore`](https://www.nuget.org/packages/Microsoft.FeatureManagement.AspNetCore/) package to version **3.0.0** or later.
2327

24-
## Create a web application with feature flags and authentication
28+
## Create a web application with authentication and feature flags
2529

26-
To roll out features based on users and groups, you need a web application that allows users to sign in.
30+
In this section, you will create a web application that allows users to sign in and use the **Beta** feature flag you created before. Most of steps is very similar to what you have done in [Quickstart](./quickstart-feature-flag-aspnet-core.md).
2731

2832
1. Create a web application that authenticates against a local database using the following command.
2933

3034
```dotnetcli
3135
dotnet new mvc --auth Individual -o TestFeatureFlags
3236
```
3337

34-
1. Build and run. Then select the **Register** link in the upper right corner to create a new user account. Use an email address of `[email protected]`. On the **Register Confirmation** screen, select **Click here to confirm your account**.
38+
1. Add references to the following NuGet packages.
39+
40+
```dotnetcli
41+
dotnet add package Microsoft.Azure.AppConfiguration.AspNetCore
42+
dotnet add package Microsoft.FeatureManagement.AspNetCore
43+
```
44+
45+
1. Store the connection string for you App Configuration store.
46+
47+
```dotnetcli
48+
dotnet user-secrets init
49+
dotnet user-secrets set ConnectionStrings:AppConfig "<your_connection_string>"
50+
```
51+
52+
1. Update the *Program.cs* with the following code.
53+
54+
``` C#
55+
// Existing code in Program.cs
56+
// ... ...
57+
58+
// Retrieve the connection string
59+
string connectionString = builder.Configuration.GetConnectionString("AppConfig");
60+
61+
// Load configuration from Azure App Configuration
62+
builder.Configuration.AddAzureAppConfiguration(options =>
63+
{
64+
options.Connect(connectionString);
65+
options.UseFeatureFlags();
66+
});
67+
68+
// The rest of existing code in program.cs
69+
// ... ...
70+
```
71+
72+
``` C#
73+
// Existing code in Program.cs
74+
// ... ...
75+
76+
// Add Azure App Configuration middleware to the container of services.
77+
builder.Services.AddAzureAppConfiguration();
78+
// Add feature management to the container of services.
79+
builder.Services.AddFeatureManagement()
80+
81+
// The rest of existing code in program.cs
82+
// ... ...
83+
```
84+
85+
``` C#
86+
// Existing code in Program.cs
87+
// ... ...
88+
89+
// Use Azure App Configuration middleware for dynamic configuration refresh.
90+
app.UseAzureAppConfiguration();
91+
92+
// The rest of existing code in program.cs
93+
// ... ...
94+
```
3595
36-
1. Follow the instructions in the [Quickstart](./quickstart-feature-flag-aspnet-core.md) to add a feature flag to your new web application.
96+
1. Add *Beta.cshtml* under the *Views\Home* directory and update it with the following markup.
97+
98+
``` cshtml
99+
@{
100+
ViewData["Title"] = "Beta Page";
101+
}
102+
103+
<h1>This is the beta website.</h1>
104+
```
105+
106+
1. Open the *HomeController.cs* under the *Controllers* directory and update it with the following code.
107+
108+
``` C#
109+
public IActionResult Beta()
110+
{
111+
return View();
112+
}
113+
```
114+
115+
1. Open *_ViewImports.cshtml*, and register the feature manager Tag Helper using an `@addTagHelper` directive:
116+
117+
``` cshtml
118+
@addTagHelper *, Microsoft.FeatureManagement.AspNetCore
119+
```
120+
121+
1. Open *_Layout.cshtml* in the Views\Shared directory. Insert a new `<feature>` tag in between the *Home* and *Privacy* navbar items.
122+
123+
``` html
124+
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
125+
<ul class="navbar-nav flex-grow-1">
126+
<li class="nav-item">
127+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
128+
</li>
129+
<feature name="Beta">
130+
<li class="nav-item">
131+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Beta">Beta</a>
132+
</li>
133+
</feature>
134+
<li class="nav-item">
135+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
136+
</li>
137+
</ul>
138+
<partial name="_LoginPartial" />
139+
</div>
140+
```
141+
142+
1. Build and run. Then select the **Register** link in the upper right corner to create a new user account. Use an email address of `[email protected]`. On the **Register Confirmation** screen, select **Click here to confirm your account**.
37143
38144
1. Toggle the feature flag in App Configuration. Validate that this action controls the visibility of the **Beta** item on the navigation bar.
39145
40-
## Update the web application code to use TargetingFilter
146+
## Update the web application code to use `TargetingFilter`
41147
42148
At this point, you can use the feature flag to enable or disable the `Beta` feature for all users. To enable the feature flag for some users while disabling it for others, update your code to use `TargetingFilter`. In this example, you use the signed-in user's email address as the user ID, and the domain name portion of the email address as the group. You add the user and group to the `TargetingContext`. The `TargetingFilter` uses this context to determine the state of the feature flag for each request.
43149
@@ -92,15 +198,7 @@ At this point, you can use the feature flag to enable or disable the `Beta` feat
92198
}
93199
```
94200

95-
1. In *Program.cs*, add a reference to the *Microsoft.FeatureManagement.FeatureFilters* namespace.
96-
97-
```csharp
98-
using Microsoft.FeatureManagement.FeatureFilters;
99-
```
100-
101-
1. Register `TargetingFilter` and `TestTargetingContextAccessor` created in the earlier step to the service collection. The `TargetingFilter` will use the `TestTargetingContextAccessor` to determine the targeting context every time that the feature flag is evaluated.
102-
103-
Since `Microsoft.FeatureManagement` 3.0.0, you can use `WithTargeting` method to register `TargetingFilter` and `ITargetingContextAccessor` at the same time.
201+
1. Add `TestTargetingContextAccessor` created in the earlier step and `TargetingFilter` to the service collection by calling the `WithTargeting` method. The `TargetingFilter` will use the `TestTargetingContextAccessor` to determine the targeting context every time that the feature flag is evaluated.
104202

105203
```csharp
106204
services.AddFeatureManagement()

0 commit comments

Comments
 (0)