Opting in controllers to show up on Help Page

By default when you install the Help Page nuget package and launch the help view, you would see help for all the controllers and their actions that are part of your application. There might be requirements where you might not want to expose all the controllers/actions on the help page. You have couple of options to achieve this.

Opt-out model:

There is  an attribute called ApiExplorerSettings which enables you to opt-out controllers and actions from showing up on the help page. Examples below:

 [ApiExplorerSettings(IgnoreApi = true)]
     public class ValuesController : ApiController
     {
         // GET api/values
         public IEnumerable<string> Get()
         {
             return new string[] { "value1", "value2" };
         }
  
         // GET api/values/5
         public string Get(int id)
         {
             return "value";
         }
     }
  
     public class AbcController : ApiController
     {
         [ApiExplorerSettings(IgnoreApi = true)]
         public IEnumerable<string> Get()
         {
             return new string[] { "value1", "value2" };
         }
  
         // GET api/values/5
         public string Get(int id)
         {
             return "value";
         }
     }

 

Opt-in model:

With opt-in model, you need not decorate your controllers/actions with attributes. You could have all the logic in one central place.

 public class OptInApiExplorer : ApiExplorer
     {
         public OptInApiExplorer(HttpConfiguration config)
             : base(config)
         {
         }
  
         public override bool ShouldExploreController(string controllerVariableValue, HttpControllerDescriptor controllerDescriptor, IHttpRoute route)
         {
             // show help for ValuesController only
             if (controllerDescriptor.ControllerType == typeof(ValuesController))
             {
                 return true;
             }
  
             return false;
         }
  
         // I overrode this method just to demonstrate the presence of this overridable behavior
         // For the current discussion, i am going to leave it as it is.
         public override Collection<HttpMethod> GetHttpMethodsSupportedByAction(IHttpRoute route, HttpActionDescriptor actionDescriptor)
         {
             return base.GetHttpMethodsSupportedByAction(route, actionDescriptor);
         }
     }

Replace the service on configuration:

 config.Services.Replace(typeof(IApiExplorer), new OptInApiExplorer(config));