Skip to content

Commit d579ba5

Browse files
reorganize aspnet targeting example
1 parent 211a185 commit d579ba5

9 files changed

+61
-72
lines changed

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

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ ms.date: 03/26/2024
1212

1313
# Tutorial: Roll out features to targeted audiences in an ASP.NET Core app
1414

15-
In this tutorial, you'll use the targeting filter to roll out a feature to targeted audience for your ASP.NET Core app. For more information about the targeting filter, please read this [article](./howto-targetingfilter.md).
15+
In this tutorial, you'll use the targeting filter to roll out a feature to targeted audience for your ASP.NET Core app. For more information about the targeting filter, see [Roll out features to targeted audiences](./howto-targetingfilter.md).
1616

1717
## Prerequisites
1818

19-
- Finish the [Quickstart: Add feature flags to an ASP.NET Core app](./quickstart-feature-flag-aspnet-core.md).
20-
- Update the [`Microsoft.FeatureManagement.AspNetCore`](https://www.nuget.org/packages/Microsoft.FeatureManagement.AspNetCore/) package to version **3.0.0** or later.
19+
- [A feature flag with targeting filter](./howto-targetingfilter.md)
20+
- [.NET SDK 6.0 or later](https://dotnet.microsoft.com/download)
2121

22-
## Create a web application with authentication and feature flags
22+
## Create a web application with a feature flag
2323

2424
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 the steps are very similar to what you have done in [Quickstart](./quickstart-feature-flag-aspnet-core.md).
2525

2626
1. Create a web application that authenticates against a local database using the following command.
2727

2828
```dotnetcli
29-
dotnet new mvc --auth Individual -o TestFeatureFlags
29+
dotnet new webapp --auth Individual -o TestFeatureFlags
3030
```
3131

3232
1. Add references to the following NuGet packages.
@@ -43,7 +43,9 @@ In this section, you will create a web application that allows users to sign in
4343
dotnet user-secrets set ConnectionStrings:AppConfig "<your_connection_string>"
4444
```
4545
46-
1. Update the *Program.cs* file with the following code.
46+
1. Add Azure App Configuration and feature management to your app.
47+
48+
Update the *Program.cs* file with the following code.
4749
4850
``` C#
4951
// Existing code in Program.cs
@@ -52,9 +54,9 @@ In this section, you will create a web application that allows users to sign in
5254
var builder = WebApplication.CreateBuilder(args);
5355
5456
// Retrieve the App Config connection string
55-
string AppConfigConnectionString = builder.Configuration.GetConnectionString("AppConfig");
57+
string AppConfigConnectionString = builder.Configuration.GetConnectionString("AppConfig") ?? throw new InvalidOperationException("Connection string 'AppConfig' not found."); ;
5658
57-
// Load configuration from Azure App Configuration
59+
// Load feature flag configuration from Azure App Configuration
5860
builder.Configuration.AddAzureAppConfiguration(options =>
5961
{
6062
options.Connect(AppConfigConnectionString);
@@ -71,6 +73,10 @@ In this section, you will create a web application that allows users to sign in
7173
// ... ...
7274
```
7375
76+
1. Call the `UseAzureAppConfiguration` method. It enables your app to use the App Configuration middleware to update the feature flag configuration for you automatically.
77+
78+
Update Program.cs withe the following code.
79+
7480
``` C#
7581
// Existing code in Program.cs
7682
// ... ...
@@ -84,68 +90,54 @@ In this section, you will create a web application that allows users to sign in
8490
// ... ...
8591
```
8692
87-
1. Add a *Beta.cshtml* file under the *Views\Home* directory and update it with the following markup.
93+
1. Add a new empty Razor page named **Beta** under the Pages directory. It includes two files *Beta.cshtml* and *Beta.cshtml.cs*.
8894
8995
``` cshtml
96+
@page
97+
@model TestFeatureFlags.Pages.BetaModel
9098
@{
9199
ViewData["Title"] = "Beta Page";
92100
}
93101
94102
<h1>This is the beta website.</h1>
95103
```
96104
97-
1. Open the *HomeController.cs* file under the *Controllers* directory and update it with the following code.
105+
1. Open *Beta.cshtml.cs*, and add `FeatureGate` attribute to the `BetaModel` class.
98106
99107
``` C#
100-
public IActionResult Beta()
108+
using Microsoft.AspNetCore.Mvc.RazorPages;
109+
using Microsoft.FeatureManagement.Mvc;
110+
111+
namespace TestFeatureFlags.Pages
101112
{
102-
return View();
113+
[FeatureGate("Beta")]
114+
public class BetaModel : PageModel
115+
{
116+
public void OnGet()
117+
{
118+
}
119+
}
103120
}
104121
```
105122
106-
1. Open the *_ViewImports.cshtml* file, and register the feature manager Tag Helper using an `@addTagHelper` directive:
123+
1. Open *Pages/_ViewImports.cshtml*, and register the feature manager Tag Helper using an `@addTagHelper` directive.
107124
108125
``` cshtml
109126
@addTagHelper *, Microsoft.FeatureManagement.AspNetCore
110127
```
111128
112-
1. Open the *_Layout.cshtml* file in the Views\Shared directory. Insert a new `<feature>` tag in between the *Home* and *Privacy* navbar items.
113-
114-
``` html
115-
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
116-
<ul class="navbar-nav flex-grow-1">
117-
<li class="nav-item">
118-
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
119-
</li>
120-
<feature name="Beta">
121-
<li class="nav-item">
122-
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Beta">Beta</a>
123-
</li>
124-
</feature>
125-
<li class="nav-item">
126-
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
127-
</li>
128-
</ul>
129-
<partial name="_LoginPartial" />
130-
</div>
131-
```
132-
133-
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**.
129+
1. Open *_Layout.cshtml* in the *Pages/Shared* directory. Insert a new `<feature>` tag in between the *Home* and *Privacy* navbar items, as shown in the highlighted lines below.
134130
135-
1. Toggle the feature flag in App Configuration. Validate that this action controls the visibility of the **Beta** item on the navigation bar.
131+
:::code language="html" source="../../includes/azure-app-configuration-navbar.md" range="15-38" highlight="13-17":::
136132
137-
## Update the web application code to use `TargetingFilter`
133+
## Enable targeting for the web application
138134
139-
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.
135+
The targeting filter evaluates a user's feature state based on the user's targeting context, which comprises the user ID and the groups the user belongs to. In this example, you use the signed-in user's email address as the user ID and the domain name of the email address as the group.
140136
141-
1. Add an *ExampleTargetingContextAccessor.cs* file with the following code.
137+
1. Add an *ExampleTargetingContextAccessor.cs* file with the following code. You implement the `ITargetingContextAccessor` interface to provide the targeting context for the signed-in user of the current request.
142138
143139
```csharp
144-
using Microsoft.AspNetCore.Http;
145140
using Microsoft.FeatureManagement.FeatureFilters;
146-
using System;
147-
using System.Collections.Generic;
148-
using System.Threading.Tasks;
149141
150142
namespace TestFeatureFlags
151143
{
@@ -183,7 +175,7 @@ At this point, you can use the feature flag to enable or disable the `Beta` feat
183175
}
184176
```
185177
186-
1. Open the *Program.cs* file and add the `ExampleTargetingContextAccessor` created in the earlier step and `TargetingFilter` to the service collection by calling the `WithTargeting` method after the existing line of `AddFeatureManagement`. The `TargetingFilter` will use the `ExampleTargetingContextAccessor` to determine the targeting context every time that the feature flag is evaluated.
178+
1. Open the *Program.cs* file and enable the targeting filter by calling the `WithTargeting` method. You pass in the type `ExampleTargetingContextAccessor` that the targeting filter will use to get the targeting context during feature flag evaluation. Add `HttpContextAccessor` to the service collection to allow `ExampleTargetingContextAccessor` to access the signed-in user information from the `HttpContext`.
187179
188180
```csharp
189181
// Existing code in Program.cs
@@ -203,20 +195,34 @@ At this point, you can use the feature flag to enable or disable the `Beta` feat
203195
> [!NOTE]
204196
> For Blazor applications, see [instructions](./faq.yml#how-to-enable-feature-management-in-blazor-applications-or-as-scoped-services-in--net-applications) for enabling feature management as scoped services.
205197
206-
1. Follow the instructions in [Roll out features to targeted audiences](./howto-targetingfilter.md) to add a targeting filter with a set of targeting rules for the *Beta* feature flag.
207-
208198
## Targeting filter in action
209199
210-
To see the effects of added targeting filter to the feature flag, build and run the application again. Initially, the **Beta** item doesn't appear on the toolbar, because the _Default percentage_ option is set to 0.
200+
1. Build and run the web app. Initially, the **Beta** item doesn't appear on the toolbar, because the _Default percentage_ option is set to 0.
211201
212-
Now sign in as `[email protected]`, using the password you set when registering. The **Beta** item now appears on the toolbar, because `[email protected]` is specified as a targeted user.
202+
> [!div class="mx-imgBorder"]
203+
> ![User not logged in and Beta item not displayed](./media/feature-filters/beta-not-targeted-by-default.png)
213204
214-
Now sign in as `[email protected]`, using the password you set when registering. The **Beta** item doesn't appear on the toolbar, because `[email protected]` is specified as an excluded user.
205+
1. Select the **Register** link in the upper right corner to create a new user account. Use an email address of `[email protected]`.
206+
207+
> [!div class="mx-imgBorder"]
208+
> ![Register a new account](./media/feature-filters/register-a-new-account.png)
209+
210+
On the **Register Confirmation** screen, select **Click here to confirm your account**.
211+
212+
> [!div class="mx-imgBorder"]
213+
> ![Register confirmation](./media/feature-filters/register-confirmation.png)
215214
216-
The following video shows this behavior in action.
215+
1. Now sign in as `[email protected]`, using the password you set when registering.
217216
218217
> [!div class="mx-imgBorder"]
219-
> ![TargetingFilter in action](./media/feature-flags-targetingfilter.gif)
218+
> ![Login an account](./media/feature-filters/login-an-account.png)
219+
220+
The **Beta** item now appears on the toolbar, because `[email protected]` is specified as a targeted user.
221+
222+
> [!div class="mx-imgBorder"]
223+
> ![User logged in and Beta item displayed](./media/feature-filters/beta-targeted-by-user.png)
224+
225+
Now sign in as `[email protected]`, using the password you set when registering. The **Beta** item doesn't appear on the toolbar, because `[email protected]` is specified as an excluded user.
220226
221227
You can create more users with `@contoso.com` and `@contoso-xyz.com` email addresses to see the behavior of the group settings.
222228

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Targeting is a feature management strategy that enables developers to progressiv
2222

2323
In this article, you will learn how to add and configure a targeting filter for your feature flags.
2424

25-
## Add a targeting filter to a feature flag
25+
## Add a targeting filter
2626

2727
1. Create a feature flag named *Beta* in your App Configuration store and open to edit it. For more information about how to add and edit a feature flag, see [Manage feature flags](./manage-feature-flags.md).
2828

81 KB
Loading
85.6 KB
Loading
186 KB
Loading
171 KB
Loading
126 KB
Loading

articles/azure-app-configuration/quickstart-feature-flag-aspnet-core.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,15 @@ Add a feature flag called *Beta* to the App Configuration store and leave **Labe
130130
}
131131
```
132132
133-
1. Open *Pages/_ViewImports.cshtml*, and register the feature manager Tag Helper using an `@addTagHelper` directive:
133+
1. Open *Pages/_ViewImports.cshtml*, and register the feature manager Tag Helper using an `@addTagHelper` directive.
134134
135135
```cshtml
136136
@addTagHelper *, Microsoft.FeatureManagement.AspNetCore
137137
```
138138
139139
The preceding code allows the `<feature>` Tag Helper to be used in the project's *.cshtml* files.
140140
141-
1. Open *_Layout.cshtml* in the *Pages*\\*Shared* directory. Insert a new `<feature>` tag in between the *Home* and *Privacy* navbar items, as shown in the highlighted lines below.
141+
1. Open *_Layout.cshtml* in the *Pages/Shared* directory. Insert a new `<feature>` tag in between the *Home* and *Privacy* navbar items, as shown in the highlighted lines below.
142142
143143
:::code language="html" source="../../includes/azure-app-configuration-navbar.md" range="15-38" highlight="13-17":::
144144

articles/azure-app-configuration/use-feature-flags-dotnet-core.md

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ In this tutorial, you will learn how to:
3030

3131
- The [Add feature flags to an ASP.NET Core app Quickstart](./quickstart-feature-flag-aspnet-core.md) shows a simple example of how to use feature flags in an ASP.NET Core application. This tutorial shows additional setup options and capabilities of the Feature Management libraries. You can use the sample app created in the quickstart to try out the sample code shown in this tutorial.
3232

33-
- Install the [`Microsoft.FeatureManagement.AspNetCore`](https://www.nuget.org/packages/Microsoft.FeatureManagement.AspNetCore/) package of version **3.0.0** or later.
34-
3533
## Set up feature management
3634

3735
To access the .NET feature manager, your app must have references to the `Microsoft.Azure.AppConfiguration.AspNetCore` and `Microsoft.FeatureManagement.AspNetCore` NuGet packages.
@@ -55,22 +53,7 @@ using Microsoft.FeatureManagement;
5553
builder.Services.AddFeatureManagement(Configuration.GetSection("MyFeatureFlags"));
5654
```
5755

58-
You can use [feature filters](./howto-feature-filters.md#tutorial-enable-conditional-features-with-feature-filters) for your feature flags. Starting with version *3.0.0* of `Microsoft.FeatureManagement`, the following [built-in filters](https://github.com/microsoft/FeatureManagement-Dotnet#built-in-feature-filters) are registered automatically as part of the `AddFeatureManagement` call, so you don't need to register them.
59-
60-
- `TimeWindowFilter`
61-
- `ContextualTargetingFilter`
62-
- `PercentageFilter`
63-
64-
You can also create your own feature filter that implements the `Microsoft.FeatureManagement.IFeatureFilter` interface. For more information, see [Implementing a Feature Filter](https://github.com/microsoft/FeatureManagement-Dotnet#implementing-a-feature-filter). To use custom filters, you must include the [Microsoft.FeatureManagement.FeatureFilters](/dotnet/api/microsoft.featuremanagement.featurefilters) namespace and add a call to [AddFeatureFilter](/dotnet/api/microsoft.featuremanagement.ifeaturemanagementbuilder.addfeaturefilter) specifying the type name of the filter you want to use as the generic type of the method.
65-
66-
The following example shows how to use a custom feature filter called `BrowserFilter`. You can find more details in this [tutorial](./howto-feature-filters-aspnet-core.md).
67-
68-
```csharp
69-
using Microsoft.FeatureManagement;
70-
71-
builder.Services.AddFeatureManagement()
72-
.AddFeatureFilter<BrowserFilter>();
73-
```
56+
You can use feature filters to enable conditional feature flags. To use either built-in feature filters or create your own, see [Enable conditional features with feature filters](./howto-feature-filters.md).
7457

7558
Rather than hard coding your feature flags into your application, we recommend that you keep feature flags outside the application and manage them separately. Doing so allows you to modify flag states at any time and have those changes take effect in the application right away. The Azure App Configuration service provides a dedicated portal UI for managing all of your feature flags. The Azure App Configuration service also delivers the feature flags to your application directly through its .NET client libraries.
7659

0 commit comments

Comments
 (0)