You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/azure-app-configuration/howto-targetingfilter-aspnet-core.md
+57-51Lines changed: 57 additions & 51 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,21 +12,21 @@ ms.date: 03/26/2024
12
12
13
13
# Tutorial: Roll out features to targeted audiences in an ASP.NET Core app
14
14
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).
16
16
17
17
## Prerequisites
18
18
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)
21
21
22
-
## Create a web application with authentication and feature flags
22
+
## Create a web application with a feature flag
23
23
24
24
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).
25
25
26
26
1. Create a web application that authenticates against a local database using the following command.
27
27
28
28
```dotnetcli
29
-
dotnet new mvc --auth Individual -o TestFeatureFlags
29
+
dotnet new webapp --auth Individual -o TestFeatureFlags
30
30
```
31
31
32
32
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
43
43
dotnet user-secrets set ConnectionStrings:AppConfig "<your_connection_string>"
44
44
```
45
45
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.
47
49
48
50
``` C#
49
51
// Existing code in Program.cs
@@ -52,9 +54,9 @@ In this section, you will create a web application that allows users to sign in
@@ -71,6 +73,10 @@ In this section, you will create a web application that allows users to sign in
71
73
// ... ...
72
74
```
73
75
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
+
74
80
``` C#
75
81
// Existing code in Program.cs
76
82
// ... ...
@@ -84,68 +90,54 @@ In this section, you will create a web application that allows users to sign in
84
90
// ... ...
85
91
```
86
92
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*.
88
94
89
95
``` cshtml
96
+
@page
97
+
@model TestFeatureFlags.Pages.BetaModel
90
98
@{
91
99
ViewData["Title"] = "Beta Page";
92
100
}
93
101
94
102
<h1>This is the beta website.</h1>
95
103
```
96
104
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.
98
106
99
107
``` C#
100
-
public IActionResult Beta()
108
+
using Microsoft.AspNetCore.Mvc.RazorPages;
109
+
using Microsoft.FeatureManagement.Mvc;
110
+
111
+
namespace TestFeatureFlags.Pages
101
112
{
102
-
return View();
113
+
[FeatureGate("Beta")]
114
+
public class BetaModel : PageModel
115
+
{
116
+
public void OnGet()
117
+
{
118
+
}
119
+
}
103
120
}
104
121
```
105
122
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.
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.
134
130
135
-
1. Toggle the feature flag in App Configuration. Validate that this action controls the visibility of the **Beta** item on the navigation bar.
## Update the web application code to use `TargetingFilter`
133
+
## Enable targeting for the web application
138
134
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.
140
136
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.
142
138
143
139
```csharp
144
-
using Microsoft.AspNetCore.Http;
145
140
using Microsoft.FeatureManagement.FeatureFilters;
146
-
using System;
147
-
using System.Collections.Generic;
148
-
using System.Threading.Tasks;
149
141
150
142
namespace TestFeatureFlags
151
143
{
@@ -183,7 +175,7 @@ At this point, you can use the feature flag to enable or disable the `Beta` feat
183
175
}
184
176
```
185
177
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`.
187
179
188
180
```csharp
189
181
// 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
203
195
> [!NOTE]
204
196
> 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.
205
197
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
-
208
198
## Targeting filter in action
209
199
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.
211
201
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
+
> 
213
204
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
+
> 
209
+
210
+
On the **Register Confirmation** screen, select **Click here to confirm your account**.
The following video shows this behavior in action.
215
+
1. Now sign in as `[email protected]`, using the password you set when registering.
217
216
218
217
> [!div class="mx-imgBorder"]
219
-
> 
218
+
> 
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
+
> 
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.
220
226
221
227
You can create more users with `@contoso.com` and `@contoso-xyz.com` email addresses to see the behavior of the group settings.
Copy file name to clipboardExpand all lines: articles/azure-app-configuration/howto-targetingfilter.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ Targeting is a feature management strategy that enables developers to progressiv
22
22
23
23
In this article, you will learn how to add and configure a targeting filter for your feature flags.
24
24
25
-
## Add a targeting filter to a feature flag
25
+
## Add a targeting filter
26
26
27
27
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).
The preceding code allows the `<feature>` Tag Helper to be used in the project's *.cshtml* files.
140
140
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.
Copy file name to clipboardExpand all lines: articles/azure-app-configuration/use-feature-flags-dotnet-core.md
+1-18Lines changed: 1 addition & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,8 +30,6 @@ In this tutorial, you will learn how to:
30
30
31
31
- 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.
32
32
33
-
- Install the [`Microsoft.FeatureManagement.AspNetCore`](https://www.nuget.org/packages/Microsoft.FeatureManagement.AspNetCore/) package of version **3.0.0** or later.
34
-
35
33
## Set up feature management
36
34
37
35
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;
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
-
usingMicrosoft.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).
74
57
75
58
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.
0 commit comments