WebAPI Help Pages: disable for Production release

烂漫一生 提交于 2019-11-29 01:30:23
Kiran Challa

Web API doesn't have an out of box support with respect to web.config based enabling or disabling of helppage.

Some options you can consider:

  • Since HelpPage is installed as an MVC area, when deploying to production you could just exclude this HelpPage folder.

  • Create an action filter which returns 404 as suggested here: Conditionally disable ASP.NET MVC Controller

NOTE: for the above cases, if you are using the default Web API template, then yeah you would need additional step of display/not display the Help link from the navigation bar.

Open the Global.asax.cs,modify your code like the following snippet code:

#if DEBUG
   AreaRegistration.RegisterAllAreas();
#endif

Because the help page is in the Area named 'HelpPage',so we can ignore it by the above code in the release or production environment.

You can use directive #if DEBUG to hide your code in realase

In case someone stumbles upon this question, here's how I managed to do it.

I added the following app setting to the base Web.config file:

<add key="ExcludeHelpPage" value="false" />

I then transformed this value in my LIVE or RELEASE config file, like this:

<add key="ExcludeHelpPage" value="true" xdt:Transform="Replace" xdt:Locator="Match(key)"  />

Then I added the following code to the end of my WebApiConfig.Register method:

if (Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["ExcludeHelpPage"]))
{
    config.Routes.IgnoreRoute("help", "help");
}

This will make the help page unavailable for configs with ExcludeHelpPage set to true.

My solution for disabling ApiController controller:

  • Uses WebConfig AppSettings config flag instead of (#if DEBUG)
  • Before method is invoked ExecuteAsync intercepts the invocation and checks feature toggle (feature flag);
  • if feature is disabled, returns HTTP 410 GONE
  • If it's common for many controllers, move the code to controller's base class

The code:

public class TestController : ApiController
{
    public override Task<HttpResponseMessage> 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);
    }

Combining Farb's and Soeholm's answers you get the Help page won't display AND it throws a 404 instead of a 500.

In Global.asax.cs

#if DEBUG // Make help page unavailable on release builds
    AreaRegistration.RegisterAllAreas(); 
#endif

and then in WebApiConfig.Register

#if !DEBUG 
    config.Routes.IgnoreRoute("help", "help"); // Make help page, which is now unavailable on release builds, throw a 404 instead of a 500.
#endif

Only I didn't use DEBUG, I made it a setting and change it with an xml transform depending on where it's deployed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!