用于外部项目或程序集的Api Explorer [英] Api Explorer for external project or assembly

查看:71
本文介绍了用于外部项目或程序集的Api Explorer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Web API的Api Explorer来开始生成自己的文档html页面.我要针对的是此处描述的内容:

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一起使用.我想拥有:

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

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

这可能吗?

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

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

推荐答案

这是受的启发swashbuckle示例,但也可以仅使用 ApiExplorer .

This is inspired by a swashbuckle sample, but is also possible using only ApiExplorer.

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

You can create an OWIN TestServer, create a HttpConfiguration, run your WebApi OWIN init code, and then create the ApiExplorer from the initialized HttpConfiguration. The only problem you might have is finding the WebApi portion of the startup code in the target assembly. In my case I'm referencing the WebAPi project in the same solution, so it's easier to call the relevant startup code.

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

这篇关于用于外部项目或程序集的Api Explorer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆