What is the best way to disable ASP.NET MVC controller conditionally?
I want to have an access to the controller actions if some value in web.config is \"true\" and
Cross posting from: https://stackoverflow.com/a/43044667/257470
My solution for disabling ApiController
controller:
#if DEBUG
)ExecuteAsync
intercepts the invocation and checks feature toggle (feature flag);HTTP 410 GONE
The code:
public class TestController : ApiController
{
public override Task ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
{
var featureFlag = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["EnableTest"]);
if (featureFlag == false)
{
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.Gone));
}
return base.ExecuteAsync(controllerContext, cancellationToken);
}