Implementing Feature Management in .NET: The Lazy Way
Implementing Feature Management in .NET
Microsoft did the hard work so you don’t have to. The Microsoft.FeatureManagement library integrates directly with .NET’s configuration and dependency injection systems, which means you can get feature flags working with minimal code and a solid foundation. For the full documentation, see learn.microsoft.com.
Installation
Add the NuGet package to your project:
dotnet add package Microsoft.FeatureManagement.AspNetCore
That’s it for dependencies. No magic rituals required.
Configuration
Register the feature management services in Program.cs:
builder.Services.AddFeatureManagement();
By default, feature flags are read from the FeatureManagement section of your appsettings.json:
{
"FeatureManagement": {
"NewDashboard": true,
"ExperimentalSearch": false
}
}
Flag names are strings. Values are booleans. Simple.
Checking a Flag in Code
Inject IFeatureManager wherever you need to check a flag:
public class DashboardController : Controller
{
private readonly IFeatureManager _featureManager;
public DashboardController(IFeatureManager featureManager)
{
_featureManager = featureManager;
}
public async Task<IActionResult> Index()
{
if (await _featureManager.IsEnabledAsync("NewDashboard"))
{
return View("NewDashboard");
}
return View("OldDashboard");
}
}
That’s the whole pattern. Inject, check, branch. Repeat as needed.
Using Feature Filters
The library supports feature filters for more sophisticated scenarios — percentage rollouts, time windows, user targeting. For example, to enable a feature for a percentage of users:
{
"FeatureManagement": {
"BetaFeature": {
"EnabledFor": [
{
"Name": "Percentage",
"Parameters": {
"Value": 20
}
}
]
}
}
}
This enables BetaFeature for 20% of requests. The library handles the sampling and you handle the business logic.
Razor Tag Helpers
If you’re building a Razor-based UI, the library includes a tag helper for conditionally rendering markup:
<feature name="NewDashboard">
<div class="new-dashboard">...</div>
</feature>
Clean, readable, and no C# in your markup. Some people consider this progress.
The Lazy Way Is Often the Right Way
Rolling your own feature flag system is a rite of passage, but it’s not a good use of time. Microsoft’s library handles the infrastructure — configuration, dependency injection, filtering, tag helpers — so you can focus on the actual feature work.
Use it. Or ask Copilot like everyone else. Or go one step further and skip the config file entirely — a dedicated flag management UI means you never have to touch appsettings.json to flip a switch again.
Why reinvent the wheel when someone already built the wheel factory?