c# - 当从另一个项目内部提供 api 时,如何在 Swashbuckle/Swaggerwork 中创建 url 路径?

标签 c# rest asp.net-web-api swagger swashbuckle

全部。我正在尝试使用 Swashbuckle 包记录 WebApi 2。

如果 API 自行运行,一切都很好,即 localhost/api/swagger 将我带到 ui,将 localhost/api/swagger/docs/v1 带到 json。

然而,生产应用程序通过从另一个 - 现在是 Web 项目(主要应用程序)中的 global.asax.cs 运行该项目的 webapiconfig 方法来初始化同一个 Webapi 项目。所以 api url 看起来像 localhost/web/api 而不是 localhost/api。

现在 swashbuckle 根本不是那样工作的。

  • localhost/api/swagger 生成错误无法加载 'API.WebApiApplication',当然好
  • localhost/web/swagger = 404
  • localhost/web/api/swagger = 404

我试图到处寻找,但我只找到了解决方法。

c.RootUrl(req => req.RequestUri.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/").TrimEnd('/'));

不幸的是,它不起作用,现在也许它应该起作用,我只需要改变一些东西,但我什至不知道这个属性到底需要什么以及应该设置什么。

可能它甚至不适用 - 也许我们的设置需要其他东西或一些虚张声势的代码更改。

如果您能提供任何帮助,我将不胜感激。我真的开始喜欢 swagger(和 swashbuckle)来获取 rest 文档。

最佳答案

对于 Swashbuckle 5.x:

这似乎是由 an extension method of httpConfiguration called EnableSwagger 设置的.虚张声势 5.x migration readme请注意,这会替换 SwaggerSpecConfig。 SwaggerDocConfig RootUrl() 专门替换了 4.x 中的 ResolveBasePathUsing()。

这实际上和以前一样工作,看起来最大的变化是它被重命名并移动到 SwaggerDocConfig 中。 :

public void RootUrl(Func<HttpRequestMessage, string> rootUrlResolver)

readme 中的示例,为简洁起见进行了调整:

string myCustomBasePath = @"http://mycustombasepath.com";

httpConfiguration
    .EnableSwagger(c =>
        {
            c.RootUrl(req => myCustomBasePath);

            // The rest of your additional metadata goes here
        });

对于 Swashbuckle 4.x:

使用 SwaggerSpecConfig ResolveBasePathUsing 并让您的 lambda 读取您已知的端点。

ResolveBasePathUsing:

public SwaggerSpecConfig ResolveBasePathUsing(Func<HttpRequestMessage, string> basePathResolver);

我的 API 在负载平衡器后面,这是提供基地址的有用解决方法。这是一个使用 ResolveBasePathUsing 来解析具有已知基本路径的路径的愚蠢示例。

string myCustomBasePath = @"http://mycustombasepath.com";

SwaggerSpecConfig.Customize(c =>
{
    c.ResolveBasePathUsing((req) => myCustomBasePath);
}

为了清楚起见,我对端点进行了硬编码,但您可以在任何地方定义它。你甚至可以use the request object to attempt to cleanup your request uri指向/web/api 而不是/api。

开发商commented on this workaround去年在 GitHub 上:

The lambda takes the current HttpRequest (i.e. the request for a given Swagger ApiDeclaration) and should return a string to be used as the baseUrl for your Api. For load-balanced apps, this should return the load-balancer path.

The default implementation is as follows:

(req) => req.RequestUri.GetLeftPart(UriPartial.Authority) +  req.GetConfiguration().VirtualPathRoot.TrimEnd('/');

...

Re relative paths, the Swagger spec requires absolute paths because the URL at which the Swagger is being served need not be the URL of the actual API.

...

The lambda is passed a HttpRequestMessage instance ... you should be able to use this to get at the RequestUri etc. Another option, you could just place the host name in your web.config and have the lambda just read it from there.

关于c# - 当从另一个项目内部提供 api 时,如何在 Swashbuckle/Swaggerwork 中创建 url 路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29995706/

相关文章:

c# - C++/CLI 中使用的动态 C# 对象

c# - 如何隐藏 DataGrid 中的列?

c# - 为什么它总是抛出 ObjectDisposedException?

c# - 如何使用 WCF 自动帮助设置示例 XML?

c# - 对象不与 child 一起添加 - EF Code First

rest - 如何通过REST调用来存储应用程序的数据

rest - 将 Ambari 与非 HDP 组件一起使用

javascript - 带有斜杠分隔参数的 Ajax 请求

asp.net - 从 Owin WebApi2 返回描述性 401 消息

c# - WebApi 无法读取分块请求