问题描述
dot net core 1.0是否支持使用APIExplorer?我找不到关于它的任何文档或使用方法,有人使用过它并且可以分享一些见解吗?
Does dot net core 1.0 support use of APIExplorer? I'm unable to find any docs on it or how to use it, has anyone used it and can share some insight?
推荐答案
感谢您的回复Itay,它帮助我获得了想要的答案.对于需要使用ApiExplorer的其他人,我在这里找到了写得很好的文章在StackOverflow上.
Thank you for your response Itay, it helped me a bit getting the answer I wanted. To anyone else that needs to use the ApiExplorer, I found a well written post hereon StackOverflow.
MVC6-所有路由列表
简而言之,要获取路由,您可以使用构造函数注入将IApiDescriptionGroupCollectionProvider注入到控制器中.然后,您将在ApiDescriptionGroupCollectionProvider.ApiDescriptionGroups.Items中接收路由.仅当您将其标记为对ApiExplorer可见时,这些路线才可见.可以按控制器或使用约定完成此操作.由于我想在所有控制器上使用它,因此我使用了IApplicationModelConvention:
MVC6 - List of all routes
Short answer, to get the routes you can have the IApiDescriptionGroupCollectionProvider injected into your controller using constructor injection. You then receive the routes in ApiDescriptionGroupCollectionProvider.ApiDescriptionGroups.Items. The routes will only be visible if you mark them as visible to ApiExplorer. This can be done per controller or by using a convention. Since I want to use it on all of my controllers, I used an IApplicationModelConvention:
public class ApiExplorerVisibilityEnabledConvention : IApplicationModelConvention
{
public void Apply(ApplicationModel application)
{
foreach (var controller in application.Controllers)
{
if (controller.ApiExplorer.IsVisible == null)
{
controller.ApiExplorer.IsVisible = true;
controller.ApiExplorer.GroupName = controller.ControllerName;
}
}
}
}
然后在Startup.cs中添加约定:
Then in Startup.cs, you add the convention:
public void ConfigureServices(IServiceCollection services)
{
// other calls omitted for brevity
services.AddMvc(opt =>
{
opt.Conventions.Add(new ApiExplorerVisibilityEnabledConvention());
});
}
这篇关于DotNetCore-是否支持ApiExplorer,以及如何使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!