Skip to content

Asp.Net MVC Integration

Valdis Iljuconoks edited this page Jul 11, 2014 · 9 revisions

Overview

FeatureSwitch Asp.Net Mvc integration provides various helpers and UI to be used in Asp.Net Mvc applications.

Setup

In order to setup FeatureSwitch library you need to do some stuff. Using Asp.Net Mvc integration library setup procedure remains the same:

  var builder = new FeatureSetBuilder();
  builder.Build();

UI Control Panel

Asp.Net Mvc integration library provides a built-in minimalistic implementation of control panel where you can overview features discovered so far or what's more important - enable or disable some of the writable features on-demand. Below is sample screenshot from sandbox Asp.Net Mvc application:

You have to call mapping procedure to register route to UI Control Panel. This could be done via Owin's IAppBuilder interface or through RouteCollection class.

  [assembly: OwinStartup(typeof(Startup))]
  namespace FeatureSwitch.AspNet.Sample
  {
      public partial class Startup
      {
          public void Configuration(IAppBuilder app)
          {
              ConfigureFeatures(app);
          }
      }
  }

  public partial class Startup
  {
      public void ConfigureFeatures(IAppBuilder app)
      {
          var builder = new FeatureSetBuilder();
          builder.Build();

          app.MapFeatureSwitch();
      }
  }

or

  public class RouteConfig
  {
      public static void RegisterRoutes(RouteCollection routes)
      {
         ....
          routes.MapFeatureSwitch();
      }
  }

By default UI Control Panel is accessible under ~/FeatureSwitch/.

Customize Route for UI Control Panel

It's possible to customize route name for the control panel if it starts to conflict with something or for any other reasons. You can do it via any extension method for registering routes for control panel:

  public partial class Startup
  {
      public void ConfigureFeatures(IAppBuilder app)
      {
          var builder = new FeatureSetBuilder();
          builder.Build();

          app.MapFeatureSwitch("FancyRouteName");
      }
  }

  public class RouteConfig
  {
      public static void RegisterRoutes(RouteCollection routes)
      {
         ....
          routes.MapFeatureSwitch("FancyRouteName");
      }
  }

After this registration control panel is available under ~/FancyRouteName/.

Securing UI Control Panel

By default Mvc controller responsible for control panel is decorated with [Authorize] attribute which means that it's not available without authentication. However by default regardless of your role membership you can access control panel. If you need to restrict access to closed list of roles you can use one of the extension methods:

  public partial class Startup
  {
      public void ConfigureFeatures(IAppBuilder app)
      {
          var builder = new FeatureSetBuilder();
          builder.Build();

          app.MapFeatureSwitch().WithRoles("Administrators");
      }
  }

  public class RouteConfig
  {
      public static void RegisterRoutes(RouteCollection routes)
      {
         ....
          routes.MapFeatureSwitch().WithRoles("Administrators");
      }
  }

Which means that only users in role "Administrators" can access control panel. Multiple roles should be separated by comma - ",".

More Information

More information on extending FeatureSwitch library.