December 07, 2017 by Anuraj
ASP.NET Core Feature Toggle
This post is about implementing feature toggle in ASP.NET Core. A feature toggle(also feature switch, feature flag, feature flipper, conditional feature, etc.) is a technique in software development that attempts to provide an alternative to maintaining multiple source-code branches (known as feature branches), such that a feature can be tested even before it is completed and ready for release. Feature toggle is used to hide, enable or disable the feature during run time. For example, during the development process, a developer can enable the feature for testing and disable it for other users.
So basically a feature toggle is something like controlling features based on the configuration file. So in the configuration file you can mention which all features you need to turn on, application will read the configuration and hide the features or UI elements.
This is very simple and minimal implementation. I am using the appsettings.json to configure features. This is my appsettings.json file.
In this I have two features,
Navigation. And I am disabling the
Carousel and enabling the
Next I have created a
IFeature interface, with
IsFeatureEnabled method, which will read the configuration file and returns bool.
And here is the implementation of
In this I am checking whether a configuration value available, if yes, will parse it to boolean and returns. If the feature is not available in the configuration, it will throw a
FeatureNotFoundException. If you want to read from database or any other configuration file, you need to modify the implementation. You can inject the class to the controllers via ASP.NET Core DI, like this.
Instead of writing the code directly in
ConfigureServices method, I created an extension method to encapsulate it.
And you can use it in
ConfigureServices method like this.
Now you have completed the implementation part, you can consume it in the code like this.
In ASP.NET Core View’s you can do similar way. First you need to inject the
IFeature to the view and you can consume it.
Instead of injecting the
IFeature on every view, you can inject it in
_ViewImports.cshtml file and all the view files can access it. Here is the
I am not a big fan of mixing HTML and Razor C# code together. In ASP.NET Core, you can implement Tag Helpers to fix this problem. So you don’t need to inject the
IFeature in views. Here is the
Favorite tag helper, which will again IFeature will be injected to the TagHelper constructor and in
process method based on the condition check the tag will be render.
And you need to add the Tag Helper in
And you can consume it in Views like this.
You can modify the
IFeature.IsFeatureEnabled implementation to use it with Database or any other configuration source. You can also implement some business logic if required, like based on tenant or based on region / country, you want to enable or disable features.
You can find the source code in GitHub
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