Working with Feature Flags in ASP.NET Core MVC application
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.
IFeatureManagerinstance - we can check the availability of the feature using
IFeatureManagerinstance - which is injected by ASP.NET Core runtime, in controllers or in other services.
FeatureGateattribute - If we want to control the execution of an Action Method or Controller actions based on the feature we can use the
FeatureGateattribute. We can add this attribute to controller / action methods -and if the feature is not enabled - it will show a 404 page.
Featuretag helper in Views - We can use the
Featuretag 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.
CustomActionFilter class should implement the
- 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
Happy Programming :)