Skip to content

Instantly share code, notes, and snippets.

View kasunkv's full-sized avatar
💭
I may be slow to respond.

Kasun Kodagoda kasunkv

💭
I may be slow to respond.
View GitHub Profile
@kasunkv
kasunkv / HomeController.cs
Created January 16, 2020 15:05
Blocking access to Controller Action methods using FeatureGate action filter.
using Microsoft.FeatureManagement.Mvc;
namespace MusicStore.Web.Controllers
{
public class HomeController : Controller
{
...
[FeatureGate(Features.Promotions)]
public async Task<IActionResult> Promotions()
@kasunkv
kasunkv / Features.cs
Created January 16, 2020 15:15
String constants with feature names
namespace MusicStore.Shared.FeatureManagement
{
public static class Features
{
public const string Promotions = "Promotion";
public const string PromotionDiscounts = "Promotion.Discounts";
public const string UserSuggestions = "Suggestion.User";
}
}
@kasunkv
kasunkv / _ViewImports.cshtml
Created January 16, 2020 15:19
Adding tag helper to the _ViewImports.cshtml
@addTagHelper *, Microsoft.FeatureManagement.AspNetCore
@kasunkv
kasunkv / Index.cshtml
Created January 16, 2020 15:59
Using feature tag helper to wrap UI elements to hide behind feature flags
@model HomeViewModel
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<feature name="@Features.PromotionDiscounts" negate="true"><h1 class="display-4">Enjoy the latest music from your favorite artist</h1></feature>
<feature name="@Features.PromotionDiscounts"><h1 class="display-4">Enjoy 25% off for selected albums from your favorite artist</h1></feature>
...
@kasunkv
kasunkv / HomeController.cs
Created January 16, 2020 16:06
Using IFeatureManager to access feature flags
using Microsoft.FeatureManagement.Mvc;
namespace MusicStore.Web.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IAlbumService _albumService;
private readonly IFeatureManager _featureManager;
@kasunkv
kasunkv / RequirementType.cs
Created January 18, 2020 16:16
RequirementType enum
namespace Microsoft.FeatureManagement
{
public enum RequirementType
{
Any = 0, // The enabled state will be attained if any feature in the set is enabled.
All = 1 // The enabled state will be attained if all features in the set are enabled.
}
}
@kasunkv
kasunkv / HomeController.cs
Created January 18, 2020 18:24
FeatureGate attribute with RequirementType
using Microsoft.FeatureManagement.Mvc;
namespace MusicStore.Web.Controllers
{
public class HomeController : Controller
{
...
[FeatureGate(RequirementType.All, Features.Promotions, Features.PromotionDiscounts)]
public async Task<IActionResult> Promotions()
@kasunkv
kasunkv / _Layout.cshtml
Created January 18, 2020 18:58
Using the tag helper to check for multiple feature flags
<div class="container">
...
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<ul class="navbar-nav flex-grow-1">
...
<feature name="@Features.Promotions, @Features.PromotionDiscounts" requirement="All">
<li class="nav-item">
@kasunkv
kasunkv / appsettings.json
Created January 19, 2020 05:16
Enable AlwaysOn feature filter.
{
...
"FeatureManagement": {
...
"Suggestion.User": {
"EnabledFor": [
{
"Name": "AlwaysOn"
@kasunkv
kasunkv / appsettings.json
Created January 19, 2020 05:56
Enabling Microsoft.Percentage feature filter
{
...
"FeatureManagement": {
...
"Suggestion.User": {
"EnabledFor": [
{
"Name": "Microsoft.Percentage",