ASP.NET Core Feature Management
Tags: aspnetcore
Published: 2020-11-15 12:42:59


This article was originally published at: [https://www.ahmetkucukoglu.com/en/asp-net-core-feature-management-en/](https://www.ahmetkucukoglu.com/en/asp-net-core-feature-management-en/)

Feature Management provides feature management in .NET Core applications. It allows the management and querying of the active / passive status of the features of the application. For example, you can ensure that a feature you have just developed is active in a certain date range. To give another example, you can ensure that a feature you have developed is active with a certain percentage. Like A/B testing.

Let's test it by practicing. First, we need to install the package below.

```
dotnet add package Microsoft.FeatureManagement.AspNetCore -version 2.0.0
```

Let's define our features in enum.

{% gist https://gist.github.com/ahmetkucukoglu/a164790e8f68505d5bc70be0f372040b %}

Let's activate the Feature Management in the Startup.

{% gist https://gist.github.com/ahmetkucukoglu/5dc9614cbe8b73e637bf046e86f5451d %}

Because Feature Management reads features from IConfiguration, we can define features in environment variable, commandline argument or appsettings. Or, if you have a custom provider, you can define your features in the related source. We will use appsettings in our example. Let's define the feature named FeatureA.

{% gist https://gist.github.com/ahmetkucukoglu/b468e3abfbe66784741ca0cb2b1c26fc %}

We need to inject the "IFeatureManager" interface to query if the features are activated. By using the "IsEnabledAsync" method of this interface, we can query as follows if the "FeatureA" feature is activated.

{% gist https://gist.github.com/ahmetkucukoglu/3e2b44ab62623fb4d1a003d4eba80c8f %}

##### **TimeWindowFilter**

If we want the feature to be active in a certain date range, we can use the internal "TimeWindowFilter". For this, we need to activate this filter in the Startup.

{% gist https://gist.github.com/ahmetkucukoglu/0ddf5ab3be183a90130472a705fe834d %}

Let's define the feature named FeatureB in appsettings.

{% gist https://gist.github.com/ahmetkucukoglu/d04e72ca36440851007e38ea96696b5f %}

UTC Date is used as the date format. If you only give the Start parameter, the feature will be active from this date. If you only give the End parameter, the feature will be active until this date.

Again, using the "IsEnabledAsync" method of the "IFeatureManager" interface, we can query if the "FeatureB" feature gets activated as follows.

{% gist https://gist.github.com/ahmetkucukoglu/1ee10ba5a2fd586154b82ecd113d6027 %}

##### **PercentageFilter**

If we want the feature to be active with a certain percentage, we can use the internal "PercentageFilter". For this, we need to activate this filter in the Startup.

{% gist https://gist.github.com/ahmetkucukoglu/955f9f15f511aefc260dd034d100a6c2 %}

Let's define the feature named FeatureC in appsettings.

{% gist https://gist.github.com/ahmetkucukoglu/e23a650f87793f40f88c912ced9e8bd6 %}

This feature will be active in 80% of the requests.

Again, using the "IsEnabledAsync" method of the "IFeatureManager" interface, we can query if the "FeatureC" feature gets activated as follows.

{% gist https://gist.github.com/ahmetkucukoglu/eaabea0eda53efe480aa8864bfc20e7a %}

##### **Custom Filter**

Apart from these filters, you can also write your own filters. For example, a filter can be written as below for the feature to be active on a mobile device.

Let's define the feature named FeatureD in appsettings.

{% gist https://gist.github.com/ahmetkucukoglu/ec79cc2659e787cfe51173b7a61332d7 %}

Let's create "MobileFilterSettings" class to access filter parameters.

{% gist https://gist.github.com/ahmetkucukoglu/ba7368d29ad4b510de59a46ccc742886 %}

Let's write our special filter named "MobileFilter" as follows.

{% gist https://gist.github.com/ahmetkucukoglu/13d27c59da0fdd6b63392d19f0a8a4c3 %}

We need to activate this filter in the Startup.

{% gist https://gist.github.com/ahmetkucukoglu/dc0ad47e0e8a60bed52b80dd09ef3805 %}

Again, using the "IsEnabledAsync" method of the "IFeatureManager" interface, we can query if "FeatureD" feature gets activated as follows.

{% gist https://gist.github.com/ahmetkucukoglu/19b5222e76a12b90b18680a967fe631f %}

##### **Mvc Action Filter Attribute**

Instead of using the IFeatureManager interface, you can set the activation of the attribute based Controller or Action, thanks to ActionFilter.

{% gist https://gist.github.com/ahmetkucukoglu/04defe4db488acf9806286dba1e010a2 %}

##### **Razor Tag Helper**

You can use the feature razor tag if you want to display the content in the View according to the activation status of the feature.

{% gist https://gist.github.com/ahmetkucukoglu/a97c2dc498c15779e413665f5d21cc68 %}

##### **Middleware**

If you want to add middleware according to the activation status of the feature, you can use the UseMiddlewareForFeature method.

{% gist https://gist.github.com/ahmetkucukoglu/37325154936419fae899ee68196e97ec %}

You can access the sample project from [Github](https://github.com/ahmetkucukoglu/aspnetcore-feature-management).

Good luck.