Api Explorer for an external project or build

I want to use the Api Explorer Web API to start creating my own html documentation pages. What I'm trying to do is something like the one described here: http://blogs.msdn.com/b/yaohuang1/archive/2013/01/20/design-time-generation-of-help-page- or-proxy-for-asp-net-web-api.aspx

However, this project is a bit outdated and does not work with the current web api. I want to have:

  • The console program in which the api explorer base libraries are installed.
  • He takes the assembly from another project and runs Api explorer on it to get all the REST paths and methods.
  • I don’t want Api explorer or help pages to be installed in the project that I am aiming for. I just want to use the project build, and the console application will have all the necessary packages of API profiles.

Is it possible?

Is it possible to download the assembly and run the Api Explorer on it?

+4
source share
1 answer

This code is for ASP.NET Core 2.0, but it may come in handy. It relies on Swashbuckle.AspNetCore and Microsoft.AspNetCore.TestHost

IWebHostBuilder builder = new WebHostBuilder()
    .UseStartup<Startup>()
    .ConfigureServices(services =>
    {
        services.AddSwaggerGen(opts =>
        {
            opts.SwaggerDoc("doc-name", new Info { Title = "Title", Version = "v1" });
        });
    });

JsonSerializerSettings mvcSerializerSettings;
SwaggerDocument document;

using (var testServer = new TestServer(builder))
{
    IOptions<MvcJsonOptions> mvcOptions = testServer.Host.Services.GetService<IOptions<MvcJsonOptions>>();
    mvcSerializerSettings = mvcOptions.Value.SerializerSettings;

    ISwaggerProvider swaggerProvider = testServer.Host.Services.GetService<ISwaggerProvider>();
    document = swaggerProvider.GetSwagger("doc-name");
}

// Reference: Swashbuckle.AspNetCore.Swagger/Application/SwaggerSerializerFactory.cs
var settings = new JsonSerializerSettings
{
    NullValueHandling = NullValueHandling.Ignore,
    Formatting = mvcSerializerSettings.Formatting,
    ContractResolver = new SwaggerContractResolver(mvcSerializerSettings),
};

string json = JsonConvert.SerializeObject(document, settings);

Where Startupis your launch class. The project is directly referenced here, but you should be able to load the assembly and consume it accordingly.

0
source

All Articles