4

Does ASP.NET Core 1.0 support the 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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Andrej Kikelj
  • 800
  • 7
  • 11

2 Answers2

8

Itay's response helped me a bit getting the answer I wanted.

To anyone else that needs to use the ApiExplorer, Dr Rob Lang wrote an answer to How to get a list of all routes in ASP.NET Core?.

In brief, 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());     
    });
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Andrej Kikelj
  • 800
  • 7
  • 11
  • I'm having the same issue, I cannot seem to find docs about how to enable the API Explorer in general. Can you share more about this? – vanthome Jul 25 '16 at 12:24
1

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).

TylerH
  • 20,799
  • 66
  • 75
  • 101
Itay Podhajcer
  • 2,616
  • 2
  • 9
  • 14