You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Feature Flags in ASP.NET Core Web API using Microsoft.FeatureManagement v4 — simple on/off flags, percentage rollouts, targeting filters, and [FeatureGate] action guards, all wired up with zero custom infrastructure.
🚀 Support the Channel — Join on Patreon
If this sample saved you time, consider joining our Patreon community.
You'll get exclusive .NET tutorials, premium code samples, and early access to new content — all for the price of a coffee.
// ProductsController.cs — switch pricing strategy at runtimeprivateasyncTask<IPricingService>GetPricingServiceAsync(){booluseNewEngine=await_featureManager.IsEnabledAsync(FeatureFlags.NewPricingEngine);returnuseNewEngine?newNewPricingService()// 10% discount:newLegacyPricingService();// no discount}
5. Declaratively guard an endpoint with [FeatureGate]
// DashboardController.cs — returns 404 automatically when flag is disabled[HttpGet("vip")][FeatureGate(FeatureFlags.VipDashboard)]publicIActionResultGetVipDashboard(){returnOk(new{Message="Welcome to the VIP Dashboard!"});}
6. Override all flags in Development
// appsettings.Development.json — all flags ON so you can test every path"FeatureManagement": {
"NewPricingEngine": true,
"DarkModeSupport": true,
"BetaSearch": true,
"VipDashboard": true
}
📡 API Endpoints
Method
Endpoint
Description
Status Codes
GET
/api/features
List all feature flags and their current state
200
GET
/api/features/{flagName}
Get a single flag's state by name
200, 404
GET
/api/products
Get all products (pricing depends on NewPricingEngine flag)
200
GET
/api/products/{id}
Get a single product by ID
200, 404
GET
/api/search?q={query}
Search products (engine depends on BetaSearch flag)
200, 400
GET
/api/dashboard/vip
VIP dashboard — guarded by [FeatureGate(VipDashboard)]
200, 404
GET
/api/dashboard/standard
Standard dashboard — always available
200
🧪 Running Tests
dotnet test -c Release
Test Summary
Test Class
Tests
Covers
PricingServiceTests
11
Discount calculation, rounding, engine names
ProductsControllerTests
6
Flag-driven pricing, 404 handling
SearchControllerTests
5
Engine routing, query validation, response shape
FeaturesControllerTests
4
Flag status listing, single flag lookup
Total
26
26 passed, 0 failed
🤔 Key Concepts
Why Microsoft.FeatureManagement over a simple bool in config?
Feature Flags in ASP.NET Core Web API using Microsoft.FeatureManagement v4: on/off flags, percentage rollouts, targeting filters, and FeatureGate action guards.