当我具有自定义基本URL时,如何用招摇设置Swashbucle v5? [英] How do I setup Swashbucle v5 with swagger when I have a custom base url?

查看:136
本文介绍了当我具有自定义基本URL时,如何用招摇设置Swashbucle v5?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将.net API升级到.net Core 3.1,并使用Swashbuckle.AspNetcore 5.4.1.该API在ServiceFabric应用程序内运行.我发现了这个 https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1173 并尝试遵循此操作,并生成了swagger,但是如果我尝试使用Swagger UI发送请求,则请求URL的IP地址错误,因此请求失败. 在旧的Swashbuckle 4.0.1设置中,我们未指定主机,仅指定了相对的basePath.我该如何存档?

I am upgrading a .net API to .net Core 3.1 and using Swashbuckle.AspNetcore 5.4.1. The API is running inside a ServiceFabric app. I found this https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1173 and tried to follow that and swagger gets generated but if I try to use the Swagger UI to send requests the request URL is with the wrong IP so the request fail. In the old Swashbuckle 4.0.1 setup we did not specify host, only the reelative basePath. How can I archieve the same?

Startup.cs

Startup.cs

var swaggerBasePath = "/MySfApp/SfApp.ClientApi/";

app.UseSwagger(c =>
{
    c.SerializeAsV2 = serializeAsSwaggerV2;

    c.RouteTemplate = "swagger/{documentName}/swagger.json";
    c.PreSerializeFilters.Add((swaggerDoc, httpReq) =>
    {
        swaggerDoc.Servers = new List<OpenApiServer> { new OpenApiServer { Url = $"{httpReq.Scheme}://{httpReq.Host.Value}{swaggerBasePath}" } };
    });
});

app.UseSwaggerUI(options =>
{
    options.SwaggerEndpoint("api/swagger.json", "My API V1");
});

结果是Swagger UI正确加载到URL上:

The result is that the Swagger UI loads correctly on URL:

http://145.12.23.1:54000/MySfApp/SfApp.ClientApi/swagger/index.html

并在名称下说BaseUrl是:

and it says under name that BaseUrl is:

[ Base URL: 10.0.0.4:10680/MySfApp/SfApp.ClientApi/ ]

10.0.0.4:10680是ServiceFabric群集内的节点.从外部到达的正确IP是145.12.23.1:54000.在Swashbuckle的旧版本(4.0.1)中,它首先说不带IP的baseUrl:"/MySfApp/SfApp.ClientApi"

The 10.0.0.4:10680 is the node inside the ServiceFabric cluster. Correct IP to reach from outside is 145.12.23.1:54000. In the older version (4.0.1) of Swashbuckle it says baseUrl without IP first: "/MySfApp/SfApp.ClientApi"

Swagger.json位于:

Swagger.json is located at:

http://40.68.213.118:19081/MySfApp/SfApp.ClientApi/swagger/api/swagger.json

它说:

"swagger": "2.0",
... 
"host": "10.0.0.4:10680",  
"basePath": "/MySfApp/SfApp.ClientApi/",
"schemes": [
"http"
],
"paths": {
"/activity/{activityId}": {
"get"
...etc

如果我尝试从Swagger UI发送GET请求,则该请求将发送到错误的IP:

If i try to send a GET request from the Swagger UI the request is sent to wrong IP:

curl -X GET "http://10.0.0.4:10680/MySfApp/MySfApp/activity/3443"


经过一些挖掘后,我现在将设置更改为此 startup.cs


EDIT 1: After some digging I have now changed the setup to this in startup.cs

var swaggerBasePath = "/MySfApp/SfApp.ClientApi/";
app.UsePathBase($"/{swaggerBasePath}");
app.UseMvc();
app.UseSwagger(c =>
{
    c.SerializeAsV2 = serializeAsSwaggerV2;

    c.PreSerializeFilters.Add((swaggerDoc, httpReq) =>
    {
        if (!httpReq.Headers.ContainsKey("X-Original-Host")) 
            return;

        var serverUrl = $"{httpReq.Headers["X-Original-Proto"]}://" +
                        $"{httpReq.Headers["X-Original-Host"]}/" +
                        $"{httpReq.Headers["X-Original-Prefix"]}";

        swaggerDoc.Servers = new List<OpenApiServer>()
        {
            new OpenApiServer { Url = serverUrl }
        };
    });
});
app.UseSwaggerUI(options => {
    options.SwaggerEndpoint("api/swagger.json", "My API V1");
});

这现在导致Swagger UI使用baseUrl正确加载

This now leads to the Swagger UI loading properly with the baseUrl

http://145.12.23.1:54000/MySfApp/SfApp.ClientApi/swagger/index.html

以及swagger.json也可以通过正确的baseUrl正确提供.

and also swagger.json is served correctly with the correct baseUrl.

http://145.12.23.1:54000/MySfApp/SfApp.ClientApi/swagger/api/swagger.json

因此解析了错误的主机名. 感谢这个主题的想法.

So the wrong hostname is resolved. Thanks to idea from this thread.

但是,当我尝试从Swagger UI页面调用终结点时,curl URL不包含baseUrl.距离更近...但是目前无法使用Swagger UI.

However when I try to call an endpoint from the Swagger UI page, the curl URL does not include the baseUrl. So closer... but currently not possible to use Swagger UI.

curl -X GET "http://10.0.0.4:10680/activity/3443"

swagger.json没有定义主机"或"basePath".

The swagger.json does not have 'host' nor 'basePath' defined.

推荐答案

尝试一下: serverUrl = $"{httpReq.Headers["X-Forwarded-Proto"]}://" + $"{httpReq.Headers["X-Forwarded-Host"]}" + _basePath; 可以使用StatelessServiceContext的ServiceName属性设置_basePath. 请注意,X-Forwarded-Proto的原始值可能会被SF覆盖.

Try this: serverUrl = $"{httpReq.Headers["X-Forwarded-Proto"]}://" + $"{httpReq.Headers["X-Forwarded-Host"]}" + _basePath; where _basePath can be set using the ServiceName property of StatelessServiceContext. Please be noted that the original value of X-Forwarded-Proto may be overridden by SF.

这篇关于当我具有自定义基本URL时,如何用招摇设置Swashbucle v5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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