DotNetCore - is ApiExplorer supported, and how to use it?

巧了我就是萌 提交于 2019-12-06 05:19:17

问题


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?


回答1:


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 here on StackOverflow.

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;
            }
        }
    }
}

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());     
    });
}



回答2:


There's a downloadable NuGet of the ApiExplorer for Asp.Net Core: Microsoft.AspNetCore.Mvc.ApiExplorer 1.0.0

So this means that it's supported (used by Swagger/Swashbackle which are also supported AFAIK).

Hope it helps!



来源:https://stackoverflow.com/questions/38368750/dotnetcore-is-apiexplorer-supported-and-how-to-use-it

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