You can host your own IHttpControllerActivator by decorating DefaultHttpControllerActivator. Inside, just check the configuration and only create a controller if allowed.
When you return null from the Create method, the user will receive a 404 Not Found message.
My example shows that the value in the application settings (App.Config or Web.Config) is checked, but, obviously, this can mean any other state of the environment.
public class YourCustomControllerActivator : IHttpControllerActivator { private readonly IHttpControllerActivator _default = new DefaultHttpControllerActivator(); public YourCustomControllerActivator() { } public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) { if (ConfigurationManager.AppSettings["MySetting"] == "Off") {
You can enable the activator like this:
GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), new YourCustomControllerActivator());
Update
Some time has passed since I looked at this question, but if I decided to do this today, I would have changed the approach a bit and used the custom IHttpControllerSelector . This is called before the activator and makes a slightly more efficient place to enable and disable the controllers ... (although a different approach works). You must be able to decorate or inherit from DefaultHttpControllerSelector .
Mark jones
source share