Microsoft.OpenApi.Models.OpenApiDocument.SerializeAsV2无效的URI:无法确定URI的格式

问题描述 投票:0回答:1

我将api从.netcore2.1升级到3.1

api使用SwagerUI和Autorest生成客户端。

[从this question about Autorest我发现我需要使用

       app.UseSwagger(c =>
        {
            c.SerializeAsV2 = true;
        });

但是UI给出了

Fetch error
Internal Server Error v1/swagger.json

json是

{
"StatusCode": 500,
"ErrorMessage": "Internal server error: Invalid URI: The format of the URI could not be determined. nStack:
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
at Microsoft.OpenApi.Models.OpenApiDocument.WriteHostInfoV2(IOpenApiWriter writer, IList`1 servers)
at Microsoft.OpenApi.Models.OpenApiDocument.SerializeAsV2(IOpenApiWriter writer)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.RespondWithSwaggerJson(HttpResponse response, OpenApiDocument swagger)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)",
"SystemMessage": "Invalid URI: The format of the URI could not be determined."
}

[更新]

我发现提到它here

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

现在json显示

{
"StatusCode": 500,
"ErrorMessage": "Internal server error: Invalid URI: Invalid port specified.
Stack:   at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
at Microsoft.OpenApi.Models.OpenApiDocument.WriteHostInfoV2(IOpenApiWriter writer, IList`1 servers)
at Microsoft.OpenApi.Models.OpenApiDocument.SerializeAsV2(IOpenApiWriter writer)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.RespondWithSwaggerJson(HttpResponse response, OpenApiDocument swagger)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)",
"SystemMessage": "Invalid URI: Invalid port specified."
}
swagger-ui autorest swashbuckle.aspnetcore
1个回答
0
投票

以下工作

        var basePath = "/v1";
        app.UseSwagger(c =>
        {
            c.RouteTemplate = "swagger/{documentName}/swagger.json";
            c.SerializeAsV2 = true;
            c.PreSerializeFilters.Add((swaggerDoc, httpReq) =>
            {
                swaggerDoc.Servers = new List<OpenApiServer> { new OpenApiServer { Url = $"{httpReq.Scheme}://{httpReq.Host.Value}{basePath}" } };
            });

        });
© www.soinside.com 2019 - 2024. All rights reserved.