用于外部项目或装配的Api Explorer

时间:2022-06-15 23:14:10

I want to use Web API's Api Explorer in order to start generating my own documentation html pages. What I'm trying to aim at is something like 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

我想使用Web API的Api Explorer来开始生成我自己的文档html页面。我试图瞄准的是这里描述的内容:http://blogs.msdn.com/b/yaohuang1/archive/2013/01/20/design-time-generation-of-help-page-or-代理换ASP净网页api.aspx

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

但是,这个项目有点过时,不适用于当前的web api。我希望有:

  1. A console program that has api explorer core libraries installed in it.
  2. 一个控制台程序,其中安装了api explorer核心库。
  3. It takes in an assembly from another project and runs Api explorer on it to get all the REST paths and methods.
  4. 它接收来自另一个项目的程序集并在其上运行Api explorer以获取所有REST路径和方法。
  5. I don't want Api explorer or the help pages to be installed on the project I am targeting. I just want to use the assembly of the project and the console application will have all the necessary API explorer packages.
  6. 我不希望Api资源管理器或帮助页面安装在我定位的项目上。我只想使用项目的程序集,控制台应用程序将拥有所有必需的API资源管理器包。

Is this possible?

这可能吗?

Can I load an assembly and run Api explorer on it?

我可以加载程序集并在其上运行Api资源管理器吗?

1 个解决方案

#1


0  

This code is for ASP.NET Core 2.0, but it may be of use to you. It relies on Swashbuckle.AspNetCore and Microsoft.AspNetCore.TestHost:

此代码适用于ASP.NET Core 2.0,但它可能对您有用。它依赖于Swashbuckle.AspNetCore和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 Startup is your project's Startup class. Here the project is directly referenced, but you should be able to load the assembly and consume it accordingly.

Startup是您项目的Startup类。这里直接引用了项目,但您应该能够加载程序集并相应地使用它。

#1


0  

This code is for ASP.NET Core 2.0, but it may be of use to you. It relies on Swashbuckle.AspNetCore and Microsoft.AspNetCore.TestHost:

此代码适用于ASP.NET Core 2.0,但它可能对您有用。它依赖于Swashbuckle.AspNetCore和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 Startup is your project's Startup class. Here the project is directly referenced, but you should be able to load the assembly and consume it accordingly.

Startup是您项目的Startup类。这里直接引用了项目,但您应该能够加载程序集并相应地使用它。