【问题标题】:Api Explorer for external project or assembly用于外部项目或程序集的 Api Explorer
【发布时间】:2019-05-31 02:34:12
【问题描述】:

我想使用 Web API 的 Api Explorer 来开始生成我自己的文档 html 页面。我试图瞄准的是这里描述的东西: 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 一起使用。 我想要:

  1. 一个控制台程序,其中安装了 api explorer 核心库。
  2. 它从另一个项目中获取一个程序集并在其上运行 Api Explorer 以获取所有 REST 路径和方法。
  3. 我不希望在我的目标项目中安装 Api 资源管理器或帮助页面。我只想使用项目的程序集,控制台应用程序将拥有所有必要的 API 资源管理器包。

这可能吗?

我可以加载程序集并在其上运行 Api Explorer 吗?

【问题讨论】:

  • 我也有同样的问题。你找到解决办法了吗?
  • 您好,我最接近的是安装以下 Nuget 包并启用 XML 文档生成:nuget.org/packages/Microsoft.AspNet.WebApi.HelpPage
  • 谢谢拉敏。为了我的需要,我需要实际的 swagger 文件

标签: c# asp.net-mvc asp.net-web-api


【解决方案1】:

此代码适用于 ASP.NET Core 2.0,但它可能对您有用。它依赖于Swashbuckle.AspNetCoreMicrosoft.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);

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

【讨论】:

    【解决方案2】:

    这是受到swashbuckle sample 的启发,但也可以仅使用ApiExplorer

    您可以创建一个 OWIN TestServer,创建一个 HttpConfiguration,运行您的 WebApi OWIN 初始化代码,然后从初始化的 HttpConfiguration 创建 ApiExplorer。您可能遇到的唯一问题是在目标程序集中找到启动代码的 WebApi 部分。就我而言,我在同一个解决方案中引用了 WebAPi 项目,因此调用相关的启动代码更容易。

    private static Collection<ApiDescription> GetApiDescriptions()
    {
      Collection<ApiDescription> descriptions = null;
      TestServer.Create(app => {
        var httpConfiguration = new HttpConfiguration();
        // This is the call to my webapi startup method in the target project, this may be tricky to find (using reflection) in your case
        new Startup().ConfigureWebApi(app, httpConfiguration);
        var apiExplorer = new ApiExplorer(httpConfiguration);
        httpConfiguration.EnsureInitialized();
        descriptions = apiExplorer.ApiDescriptions;
      });
      return descriptions;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 2012-03-14
      相关资源
      最近更新 更多