How can i implement feature flag using Azure App Configuration for MVC project in .Net Framework

Prasanna Venkatesh Rajendran 25 Reputation points
2023-07-19T10:52:01.49+00:00
  1. "I am interested in utilizing the Azure App Configuration feature manager to implement a feature flag."
  2. "While I have come across documentation from Microsoft that explains how to implement a feature flag for a console application (https://learn.microsoft.com/en-us/azure/azure-app-configuration/quickstart-feature-flag-dotnet), I still find myself uncertain about its implementation for an MVC project in the .NET Framework."
  3. "I would appreciate guidance on implementing a feature flag for a web application in the .NET Framework. It would be great if you could provide a solution or point me towards relevant documentation for reference."
Azure App Configuration
Azure App Configuration
An Azure service that provides hosted, universal storage for Azure app configurations.
209 questions
{count} votes

Accepted answer
  1. MuthuKumaranMurugaachari-MSFT 22,246 Reputation points
    2023-07-20T21:07:11.3066667+00:00

    Prasanna Venkatesh Rajendran I have looked up references available for ASP.NET Core https://learn.microsoft.com/en-us/azure/azure-app-configuration/quickstart-feature-flag-aspnet-core?tabs=core6x and full demo code: https://github.com/microsoft/FeatureManagement-Dotnet/tree/main/examples/FeatureFlagDemo to create a similar scenario in ASP.NET MVC application. Observed that FeatureGate attribute and feature tag helper is not available in .NET Framework MVC application. Similarly, the dependency injection is a lot simpler in ASP.NET core MVC.

    Here is the thing that can get you started with MVC .NET Framework (from the scaffold code).

    • Add the following code in Global.asax.cs and make sure to add necessary dependencies such as Microsoft.FeatureManagement
    public class MvcApplication : System.Web.HttpApplication
        {
            public static IFeatureManager FeatureManager;
    
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
    
                string connString = "app config connection string";
    
                IConfigurationRoot configuration = new ConfigurationBuilder()
                .AddAzureAppConfiguration(options =>
                {
                    options.Connect(connString).UseFeatureFlags();
                }).Build();
    
                IServiceCollection services = new ServiceCollection();
    
                services.AddSingleton<IConfiguration>(configuration).AddFeatureManagement();
    
                using (ServiceProvider serviceProvider = services.BuildServiceProvider())
                {
                    FeatureManager = serviceProvider.GetRequiredService<IFeatureManager>();
                }
            }
        }
    
    • Create a custom Helper Feature Class with the following code snippet:
    public static MvcHtmlString Feature(this HtmlHelper htmlHelper, string featureName, MvcHtmlString content)
            {
                if (MvcApplication.FeatureManager.IsEnabledAsync(featureName).Result)
                {
                    return new MvcHtmlString("<li>" + content + "</li>");
                }
                else
                {
                    return MvcHtmlString.Create("");
                }                
            }
    
    • Add this helper tag @Html.Feature in _Layout.cshtml to display Beta page only when the feature is enabled.
    @Html.Feature("Beta", @Html.ActionLink("Beta", "Beta", "Beta", new { area = "" }, new { @class = "nav-link" }))
    

    Screenshot with feature enabled:
    User's image

    Screenshot with feature disabled:

    User's image

    The full source code for Feature Management is available in https://github.com/microsoft/FeatureManagement-Dotnet/tree/main/src repo and you can implement FeatureGate attribute or HTML helpers in your .NET MVC application (not readily available for .NET Framework).
    Note: this code snippet is just for reference, not production code and please make sure to validate and have dependency injection implemented with best practices.

    I hope this helps and let me know if any questions. I will submit the feedback to our product team for adding example/tutorial for .NET Framework MVC application.


    If you found the answer to your question helpful, please take a moment to mark it as "Yes" for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.


0 additional answers

Sort by: Most helpful