Prasanna Venkatesh Rajendran I have looked up references available for ASP.NET Core https://zcusa.951200.xyz/en-us/azure/azure-app-configuration/quickstart-feature-flag-aspnet-core?tabs=core6x and full demo code: https://github.com/microsoft/FeatureManagement-Dotnet/tree/main/examples/FeatureFlagDemo to create a similar scenario in ASP.NET MVC application. Observed that FeatureGate
attribute and feature
tag helper is not available in .NET Framework
MVC application. Similarly, the dependency injection is a lot simpler in ASP.NET core MVC.
Here is the thing that can get you started with MVC .NET Framework (from the scaffold code).
- Add the following code in
Global.asax.cs
and make sure to add necessary dependencies such asMicrosoft.FeatureManagement
public class MvcApplication : System.Web.HttpApplication
{
public static IFeatureManager FeatureManager;
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
string connString = "app config connection string";
IConfigurationRoot configuration = new ConfigurationBuilder()
.AddAzureAppConfiguration(options =>
{
options.Connect(connString).UseFeatureFlags();
}).Build();
IServiceCollection services = new ServiceCollection();
services.AddSingleton<IConfiguration>(configuration).AddFeatureManagement();
using (ServiceProvider serviceProvider = services.BuildServiceProvider())
{
FeatureManager = serviceProvider.GetRequiredService<IFeatureManager>();
}
}
}
- Create a custom Helper
Feature
Class with the following code snippet:
public static MvcHtmlString Feature(this HtmlHelper htmlHelper, string featureName, MvcHtmlString content)
{
if (MvcApplication.FeatureManager.IsEnabledAsync(featureName).Result)
{
return new MvcHtmlString("<li>" + content + "</li>");
}
else
{
return MvcHtmlString.Create("");
}
}
- Add this helper tag
@Html.Feature
in_Layout.cshtml
to display Beta page only when the feature is enabled.
@Html.Feature("Beta", @Html.ActionLink("Beta", "Beta", "Beta", new { area = "" }, new { @class = "nav-link" }))
Screenshot with feature enabled:
Screenshot with feature disabled:
The full source code for Feature Management is available in https://github.com/microsoft/FeatureManagement-Dotnet/tree/main/src repo and you can implement FeatureGate
attribute or HTML helpers in your .NET MVC application (not readily available for .NET Framework).
Note: this code snippet is just for reference, not production code and please make sure to validate and have dependency injection implemented with best practices.
I hope this helps and let me know if any questions. I will submit the feedback to our product team for adding example/tutorial for .NET Framework
MVC application.
If you found the answer to your question helpful, please take a moment to mark it as "Yes" for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.