Feature flags in ASP.NET Core

A screenshot of some ASP.NET code showing the tag helper for Feature Flagging being used

I've been working on a few ASP.NET Core applications recently which has given me cause and opportunity to gate some functionality access behind feature flags whilst it's a work in progress, rather than having it gated behind if (app.Environment.IsDevelopment()) or similar where I then have to make code changes to enable the functionality. I've been doing this by using the Microsoft.FeatureManagement.AspNetCore NuGet package which serves me with absolutely everything I need, at least for my relatively simple use cases. The readme on GitHub gives a pretty complete summary of how to use the package but for the sake of completness, here's a basic example excerpted from the implementation I'm using right now.

Adding support to an ASP.NET Core 6 project is as simple as adding the library and then calling the AddFeatureManagement extension method:

using Microsoft.FeatureManagement;

var builder = WebApplication.CreateBuilder(args);


With that complete it's almost trivial to gate functionality in markup in a razor view behind a flag:

<feature name="SearchInTopHat">
    <form class="d-flex float-md-end" asp-controller="home" asp-action="search">
        <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search" id="q" name="q">
        <button class="btn btn-outline-light" type="submit">Search>/button>

I've used strings for feature name (the examples in the documentation use an enum which does make for stronger typing in your code but doesn't fully eliminate the risk of typos as it doesn't flow through to the configuration file. In the configuration file the absence of a feature flag implies that it's false which I use to leave all new/un-finished features switched off in production but then enable them in development by listing them in configuration explicitly:

  "FeatureManagement": {
"SearchInTopHat": true

Documentation for the tag helper can be found at docs.microsoft.com (code: on GitHub). Finding the documentation is mildly tricky as the it's structured by namespace whereas you'll never actually reference the namespace, for the most part - the @addTagHelper directive that needs to be added to _ViewImports.cshtml is usually of the just name the assembly and pull in everything variety. However, once you've found the documentation you can see the different ways the tag helper can be wrangled. Of particular use is the 'Negate' property which allows you to show something when a feature is disabled.

I've only made use of it in a .NET Core web application but it's equally usable in other .NET Core scenarios and has a lot more to it than the surface-level functionality I've described - but that's probably what 80% of people, like me, looking for this will need. It's definitely better than baking your own feature flag functionality and far, far, quicker!

About Rob

I've been interested in computing since the day my Dad purchased his first business PC (an Amstrad PC 1640 for anyone interested) which introduced me to MS-DOS batch programming and BASIC.

My skillset has matured somewhat since then, which you'll probably see from the posts here. You can read a bit more about me on the about page of the site, or check out some of the other posts on my areas of interest.

No Comments

Add a Comment