Working with Feature Flags in ASP.NET Core MVC application
March 08, 2022 by Anuraj
Azure AspNetCore DotNetCore DevOps
This post is about Adding feature flags to an ASP.NET Core app. In this blog post we will discuss about various extension points of Feature Management package. In the last post we implemented the feature management in controller code. But that might not be the scenario always - we might like to control the feature visibility in views or in middleware. We will be able to use IFeatureManager
instance, but FeatureManagement package offers more extension points like Views, Filters and Middleware out of the box.
These are the various ways we can consume Feature management.
- Using
IsEnabledAsync
method ofIFeatureManager
instance - we can check the availability of the feature usingIsEnabledAsync
method ofIFeatureManager
instance - which is injected by ASP.NET Core runtime, in controllers or in other services.
- Using
FeatureGate
attribute - If we want to control the execution of an Action Method or Controller actions based on the feature we can use theFeatureGate
attribute. We can add this attribute to controller / action methods -and if the feature is not enabled - it will show a 404 page.
- Using
Feature
tag helper in Views - We can use theFeature
tag helper and conditionally render elements, like this.
We need to modify the ViewImports.cshtml
and add the following line to start using this tag helper.
- Conditionally execute Action Filters - we can use the Feature Management package to conditionally execute action filters, like this.
The CustomActionFilter
class should implement the IAsyncActionFilter
interface.
- Conditionally execute middleware - we can use the feature management package to conditionally execute middleware as well.
In case of controllers and views if the feature is disabled it will redirect to 404 page - it is not a nice user experience. We can customize this behavior by implementing the IDisabledFeaturesHandler
interface. Here is one simple implementation.
And we need to map the DisabledFeaturesHandler
to the Feature Management service like this.
These are the various out of the box feature management options to control the execution and rendering of ASP.NET Core MVC application code and views. You can implement your own filters as well, by implementing IFeatureFilter
interface.
Happy Programming :)
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub