Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Ignore WebAPI method on controller for api explorer documentation

we have implemented a webAPI and we have a number of API controllers. We provide an API documentation for our API and what we want to do is to exclude certain web methods from the documentation but we want this to be done dynamically depending on the environment we are running. just to give you an understanding of what I mean, let's say I have the following web method

[ApiExplorerSettings(IgnoreApi = true)] 
public Product getProduct()
{
   ...
}

By setting the IgnoreAPI property on the ApiExplorerSettingAttribute to true, it excludes the web method from the documentation which is what we want but we need a way of setting the "true" value dynamically. Ideally we would like to have a database table with bool values for each webMethod and based on these set the value for IgnoreAPi property. Is there a way to achieve this? Your help will be much appreciated.

like image 966
CodeExplorer Avatar asked Jan 14 '15 20:01

CodeExplorer


1 Answers

You can implement a custom IApiExplorer and register it in Web API's services to have full control over which APIs are listed or not.

Here's a blog post from the dev who implemented most of this: https://docs.microsoft.com/en-us/archive/blogs/yaohuang1/asp-net-web-api-introducing-iapiexplorerapiexplorer

And here's the IApiExplorer interface definition: http://msdn.microsoft.com/en-us/library/system.web.http.description.iapiexplorer(v=vs.118).aspx

One thing you could do is derive from (or re-use the existing source of) the existing ApiExplorer implementation and call base to get the default list, and then further filter it however you want.

And per s_hewitt's comment, the recommendation is:

Deriving from ApiExplorer, implementing the two methods ShouldExploreAction and ShouldExploreController is the way to go. Make your DB calls in those two methods, looking up based on route, controller and action.

like image 146
Eilon Avatar answered Oct 19 '22 16:10

Eilon