在发布版本中隐藏 api 端点

use*_*487 2 c# asp.net-web-api asp.net-mvc-apiexplorer webapi2

我正在开发一个 Asp.net Web api 项目。我最近使用创建了一个文档端点config.Services.GetApiExplorer();

在生产中隐藏此端点并仍然可供我团队中的所有其他开发人员使用的最佳方法是什么?

我能想到的一种方法是使用注册路线

#if debug

routes.MapRoute(
"documentation",
"documentation/help", 
new { controller = "apiexplorer", action
= "Index" }
);

#endif
Run Code Online (Sandbox Code Playgroud)

小智 6

There is two attribute could hide an API endpoint:

[ApiExplorerSettings(IgnoreApi = true)]
[NonAction]
public async Task<void> PrivateAPI()
{
...
}
Run Code Online (Sandbox Code Playgroud)

But for your case, I probably create a new attribute to check the environment and apply that attribute to your controller method. Inject the 'IHostingEnvironment' class, then use .IsDevelopment() method.