Azure Functions 上的 SwaggerUI(NET 8 更新)

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

我目前正在更新到 NET 8。(也切换到隔离的工作进程) 我有一个基于 https://devkimchi.com/2019/02/02/introducing-swagger-ui-on-azure-functions/ 的 SwaggerUi 实现 我的控制器就像

public static class SwaggerUiController
{
    [Function("SwaggerUiController")]
    public static async Task<IActionResult> RenderSwaggerUI(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "openapi/swaggerui")]
        HttpRequestData  req, ILogger log)
    {
        const string title = "My title 1";
        const string endpoint = "openapi/openapi.yaml";

        return await ConfigureSwaggerUI(req, title, endpoint);
    }...

还有另外 2 个函数,服务于 2 个不同的路由(并且有其他端点) 在我的ConfigureSwaggerUI 中我有

private static async Task<ContentResult> ConfigureSwaggerUI(HttpRequestData req, string title, string endpoint)
    {
        var swaggerUi = new SwaggerUI();

        var openApiInfo = CreateOpenApiInfo(title);
        var routePrefix = req.Url.Host.Contains("localhost") ? "api" : "doc";
       // req.Host = new HostString(Environment.GetEnvironmentVariable("MyApiEnvVariable") ?? "localhost:7071");

        var httpRequestObject = new HttpRequestObject(req);

        var assembly = typeof(SwaggerUI).Assembly;

        var result =
            await swaggerUi
                .AddMetadata(openApiInfo)
                .AddServer(httpRequestObject, routePrefix)
                .BuildAsync(assembly)
                .RenderAsync(endpoint)
                .ConfigureAwait(false);
        return CreateContentResult(result);
    }

更新时,我必须从 HttpRequest 更改为 HttpRequestData,这阻止了我 根据注释行设置主机

// req.Host = new HostString(Environment.GetEnvironmentVariable("MyApiEnvVariable") ?? "localhost:7071");

如何在更新到 NET 8 的同时保持 SwaggerUi 功能完好无损?

c# .net azure azure-functions swagger-ui
1个回答
1
投票

以下内容应将主机标头附加到请求中。

        httpRequestData.Headers.Add("Host", $"{Environment.GetEnvironmentVariable("MyApiEnvVariable") ?? "localhost:7071"}");
© www.soinside.com 2019 - 2024. All rights reserved.