|
| 1 | +--- |
| 2 | +title: 'Tutorial: Use variant feature flags from Azure App Configuration in an ASP.NET application' |
| 3 | +titleSuffix: Azure App configuration |
| 4 | +description: In this tutorial, you learn how to use variant feature flags in an ASP.NET application |
| 5 | +#customerintent: As a user of Azure App Configuration, I want to learn how I can use variants and variant feature flags in my ASP.NET application. |
| 6 | +author: rossgrambo |
| 7 | +ms.author: rossgrambo |
| 8 | +ms.service: azure-app-configuration |
| 9 | +ms.devlang: csharp |
| 10 | +ms.topic: tutorial |
| 11 | +ms.date: 10/18/2024 |
| 12 | +--- |
| 13 | + |
| 14 | +# Tutorial: Use variant feature flags from Azure App Configuration in an ASP.NET application |
| 15 | + |
| 16 | +In this tutorial, you use a variant feature flag to manage experiences for different user segments in an example application, *Quote of the Day*. You utilize the variant feature flag created in [Use variant feature flags](./use-variant-feature-flags.md). Before proceeding, ensure you create the variant feature flag named *Greeting* in your App Configuration store. |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +* Ensure the [.NET CLI](/dotnet/core/tools) is installed on your machine. |
| 21 | +* Follow the [Use variant feature flags](./use-variant-feature-flags.md) tutorial and create the variant feature flag named *Greeting*. |
| 22 | + |
| 23 | +## Create an ASP.NET Core web app |
| 24 | + |
| 25 | +1. Run the following code in a command prompt. This command creates a new Razor Pages application in ASP.NET Core, using Individual account auth, and places it in an output folder named *QuoteOfTheDay*. |
| 26 | + |
| 27 | + ```dotnetcli |
| 28 | + dotnet new razor --auth Individual -o QuoteOfTheDay |
| 29 | + ``` |
| 30 | +
|
| 31 | +1. Create a [user secret](/aspnet/core/security/app-secrets) for the application by navigating into the *QuoteOfTheDay* folder and run the following command. This secret holds the endpoint for your App Configuration. |
| 32 | +
|
| 33 | + ```dotnetcli |
| 34 | + dotnet user-secrets set Endpoints:AppConfiguration "<App Configuration Endpoint>" |
| 35 | + ``` |
| 36 | +
|
| 37 | +1. Add the latest versions of the required packages. |
| 38 | +
|
| 39 | + ```dotnetcli |
| 40 | + dotnet add package Azure.Identity |
| 41 | + dotnet add package Microsoft.Extensions.Configuration.AzureAppConfiguration |
| 42 | + dotnet add package Microsoft.FeatureManagement.AspNetCore |
| 43 | + ``` |
| 44 | +
|
| 45 | +## Connect to App Configuration for feature management |
| 46 | +
|
| 47 | +1. In *Program.cs*, add the following using statements. |
| 48 | +
|
| 49 | + ```csharp |
| 50 | + using Azure.Identity; |
| 51 | + using Microsoft.Extensions.Configuration.AzureAppConfiguration; |
| 52 | + using Microsoft.FeatureManagement; |
| 53 | + ``` |
| 54 | +
|
| 55 | +1. In *Program.cs*, under the line `var builder = WebApplication.CreateBuilder(args);`, add the App Configuration provider, which pulls down the configuration from Azure App Configuration when the application starts. By default, the `UseFeatureFlags` method pulls down all feature flags with no label. |
| 56 | +
|
| 57 | + You use the `DefaultAzureCredential` to authenticate to your App Configuration store. Follow the [instructions](./concept-enable-rbac.md#authentication-with-token-credentials) to assign your credential the **App Configuration Data Reader** role. Be sure to allow sufficient time for the permission to propagate before running your application. |
| 58 | +
|
| 59 | + ```csharp |
| 60 | + builder.Configuration |
| 61 | + .AddAzureAppConfiguration(options => |
| 62 | + { |
| 63 | + string endpoint = builder.Configuration.Get("Endpoints:AppConfiguration"); |
| 64 | + options.Connect(new Uri(endpoint), new DefaultAzureCredential()); |
| 65 | +
|
| 66 | + options.UseFeatureFlags(); |
| 67 | + }); |
| 68 | + ``` |
| 69 | +
|
| 70 | +1. Add Azure App Configuration and feature management services and enable targeting for feature management. |
| 71 | +
|
| 72 | + ```csharp |
| 73 | + // Add Azure App Configuration and feature management services to the container. |
| 74 | + builder.Services.AddAzureAppConfiguration() |
| 75 | + .AddFeatureManagement() |
| 76 | + .WithTargeting(); |
| 77 | + ``` |
| 78 | +
|
| 79 | +1. Under the line `var app = builder.Build();`, add Azure App Configuration middleware for dynamic configuration refresh. |
| 80 | +
|
| 81 | + ```csharp |
| 82 | + // Use Azure App Configuration middleware for dynamic configuration refresh. |
| 83 | + app.UseAzureAppConfiguration(); |
| 84 | + ``` |
| 85 | +
|
| 86 | +## Use the variant feature flag |
| 87 | +
|
| 88 | +1. Open *QuoteOfTheDay* > *Pages* > *Index.cshtml.cs* and replace the content with the following code. |
| 89 | +
|
| 90 | + ```csharp |
| 91 | + using Microsoft.AspNetCore.Mvc; |
| 92 | + using Microsoft.AspNetCore.Mvc.RazorPages; |
| 93 | + using Microsoft.FeatureManagement; |
| 94 | + |
| 95 | + namespace QuoteOfTheDay.Pages; |
| 96 | + |
| 97 | + public class Quote |
| 98 | + { |
| 99 | + public string Message { get; set; } |
| 100 | + |
| 101 | + public string Author { get; set; } |
| 102 | + } |
| 103 | + |
| 104 | + public class IndexModel(IVariantFeatureManagerSnapshot featureManager) : PageModel |
| 105 | + { |
| 106 | + private readonly IVariantFeatureManagerSnapshot _featureManager = featureManager; |
| 107 | + |
| 108 | + private Quote[] _quotes = [ |
| 109 | + new Quote() |
| 110 | + { |
| 111 | + Message = "You cannot change what you are, only what you do.", |
| 112 | + Author = "Philip Pullman" |
| 113 | + }]; |
| 114 | + |
| 115 | + public Quote? Quote { get; set; } |
| 116 | + |
| 117 | + public string GreetingMessage { get; set; } |
| 118 | + |
| 119 | + public async void OnGet() |
| 120 | + { |
| 121 | + Quote = _quotes[new Random().Next(_quotes.Length)]; |
| 122 | + |
| 123 | + Variant variant = await _featureManager.GetVariantAsync("Greeting", HttpContext.RequestAborted); |
| 124 | +
|
| 125 | + if (variant != null) |
| 126 | + { |
| 127 | + GreetingMessage = variant.Configuration?.Get<string>() ?? ""; |
| 128 | + } |
| 129 | + else |
| 130 | + { |
| 131 | + _logger.LogWarning("No variant given. Either the feature flag named 'Greeting' is not defined or the variants are not defined properly."); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + ``` |
| 136 | +
|
| 137 | + You call `GetVariantAsync` to retrieve the variant of the *Greeting* feature flag for the current user and assign its value to the `GreetingMessage` property of the page model. |
| 138 | +
|
| 139 | +1. In *QuoteOfTheDay* > *Pages* > *Shared* > *_Layout.cshtml*, under where `QuoteOfTheDay.styles.css` is added, add the following reference to the font-awesome CSS library. |
| 140 | +
|
| 141 | + ```css |
| 142 | + <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"> |
| 143 | + ``` |
| 144 | +
|
| 145 | +1. Open *index.cshtml* and replace its content with the following code. |
| 146 | +
|
| 147 | + ```cshtml |
| 148 | + @page |
| 149 | + @model IndexModel |
| 150 | + @{ |
| 151 | + ViewData["Title"] = "Home page"; |
| 152 | + ViewData["Username"] = User.Identity?.Name ?? string.Empty; |
| 153 | + } |
| 154 | + |
| 155 | + <style> |
| 156 | + body { |
| 157 | + font-family: Arial, sans-serif; |
| 158 | + background-color: #f4f4f4; |
| 159 | + color: #333; |
| 160 | + } |
| 161 | + |
| 162 | + .quote-container { |
| 163 | + background-color: #fff; |
| 164 | + margin: 2em auto; |
| 165 | + padding: 2em; |
| 166 | + border-radius: 8px; |
| 167 | + max-width: 750px; |
| 168 | + box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2); |
| 169 | + display: flex; |
| 170 | + justify-content: space-between; |
| 171 | + align-items: start; |
| 172 | + position: relative; |
| 173 | + } |
| 174 | + |
| 175 | + .vote-container { |
| 176 | + position: absolute; |
| 177 | + top: 10px; |
| 178 | + right: 10px; |
| 179 | + display: flex; |
| 180 | + gap: 0em; |
| 181 | + } |
| 182 | + |
| 183 | + .vote-container .btn { |
| 184 | + background-color: #ffffff; /* White background */ |
| 185 | + border-color: #ffffff; /* Light blue border */ |
| 186 | + color: #333 |
| 187 | + } |
| 188 | + |
| 189 | + .vote-container .btn:focus { |
| 190 | + outline: none; |
| 191 | + box-shadow: none; |
| 192 | + } |
| 193 | + |
| 194 | + .vote-container .btn:hover { |
| 195 | + background-color: #F0F0F0; /* Light gray background */ |
| 196 | + } |
| 197 | + |
| 198 | + .greeting-content { |
| 199 | + font-family: 'Georgia', serif; /* More artistic font */ |
| 200 | + } |
| 201 | + |
| 202 | + .quote-content p.quote { |
| 203 | + font-size: 2em; /* Bigger font size */ |
| 204 | + font-family: 'Georgia', serif; /* More artistic font */ |
| 205 | + font-style: italic; /* Italic font */ |
| 206 | + color: #4EC2F7; /* Medium-light blue color */ |
| 207 | + } |
| 208 | + </style> |
| 209 | + |
| 210 | + <div class="quote-container"> |
| 211 | + <div class="quote-content"> |
| 212 | + <h3 class="greeting-content">@(Model.GreetingMessage)</h3> |
| 213 | + <br /> |
| 214 | + <p class="quote">“@(Model.Quote?.Message ?? "< Quote not found >")”</p> |
| 215 | + <p>- <b>@(Model.Quote?.Author ?? "Unknown")</b></p> |
| 216 | + </div> |
| 217 | + |
| 218 | + <div class="vote-container"> |
| 219 | + <button class="btn btn-primary" onclick="heartClicked(this)"> |
| 220 | + <i class="far fa-heart"></i> <!-- Heart icon --> |
| 221 | + </button> |
| 222 | + </div> |
| 223 | + </div> |
| 224 | + |
| 225 | + <script> |
| 226 | + function heartClicked(button) { |
| 227 | + var icon = button.querySelector('i'); |
| 228 | + icon.classList.toggle('far'); |
| 229 | + icon.classList.toggle('fas'); |
| 230 | + } |
| 231 | + </script> |
| 232 | + ``` |
| 233 | +
|
| 234 | + This code displays the UI of the *Quote of the Day* application and shows the `GreetingMessage` from the page model. The JavaScript handler `heartClicked` is triggered when the heart button is clicked. |
| 235 | +
|
| 236 | +### Build and run the app |
| 237 | +
|
| 238 | +1. Build and run the application. |
| 239 | +
|
| 240 | + ```dotnetcli |
| 241 | + dotnet build |
| 242 | + dotnet run |
| 243 | + ``` |
| 244 | +1. Once the application is loaded, select **Register** at the top right to register a new user. |
| 245 | +
|
| 246 | + :::image type="content" source="media/use-variant-feature-flags-aspnet-core/register.png" alt-text="Screenshot of the Quote of the day app, showing Register."::: |
| 247 | +
|
| 248 | +1. Register a new user named *[email protected]*. |
| 249 | +
|
| 250 | +1. Select the link **Click here to validate email** after entering user information. |
| 251 | +
|
| 252 | + :::image type="content" source="media/use-variant-feature-flags-aspnet-core/click-to-confirm.png" alt-text="Screenshot of the Quote of the day app, showing click to confirm."::: |
| 253 | +
|
| 254 | +1. Repeat the same steps to register a second user named *[email protected]*. |
| 255 | +
|
| 256 | + > [!NOTE] |
| 257 | + > It's important for the purpose of this tutorial to use these names exactly. As long as the feature has been configured as expected, the two users should see different variants. |
| 258 | +
|
| 259 | +1. Select **Login** at the top right to sign in as [email protected]. |
| 260 | +
|
| 261 | + :::image type="content" source="media/use-variant-feature-flags-aspnet-core/login.png" alt-text="Screenshot of the Quote of the day app, showing **Login**."::: |
| 262 | +
|
| 263 | +1. Once logged in, you see a long greeting message for **[email protected]** |
| 264 | +
|
| 265 | + :::image type="content" source="media/use-variant-feature-flags-aspnet-core/long-variant.png" alt-text="Screenshot of the Quote of the day app, showing a long message for the user."::: |
| 266 | +
|
| 267 | +1. Click *Logout* and login as **[email protected]**, you see the simple greeting message. |
| 268 | +
|
| 269 | + :::image type="content" source="media/use-variant-feature-flags-aspnet-core/simple-variant.png" alt-text="Screenshot of the Quote of the day app, showing a simple message for the user."::: |
| 270 | +
|
| 271 | +## Next steps |
| 272 | +
|
| 273 | +For the full feature rundown of the .NET feature management library, refer to the following document. |
| 274 | +
|
| 275 | +> [!div class="nextstepaction"] |
| 276 | +> [.NET Feature Management](./feature-management-dotnet-reference.md) |
0 commit comments