gpt4 book ai didi

azure - 更改 .NET 6 隔离函数的 OpenAPI 的默认 url

转载 作者:行者123 更新时间:2023-12-03 06:09:26 30 4
gpt4 key购买 nike

我有一个 .NET 6.0 隔离函数,并为其添加了 OpenAPI 支持。这是我的 Program.cs 的片段:

var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults(worker => worker.UseNewtonsoftJson())
.ConfigureServices(services =>
{
services.AddSingleton<IOpenApiConfigurationOptions>(_ =>
{
var options = new OpenApiConfigurationOptions()
{
Info = new OpenApiInfo()
{
Version = "1.0.0",
Title = "Function Name",
Description = "This is the name of the function",
Contact = new OpenApiContact()
{
Name = "Codebug"
}
},
Servers = DefaultOpenApiConfigurationOptions.GetHostNames(),
OpenApiVersion = OpenApiVersionType.V2,
IncludeRequestingHostName = true,
ForceHttps = false,
ForceHttp = false,
};

return options;
});
})
.Build();

host.Run();

这可以正常工作,没有任何问题。当我在控制台中运行该函数时,我会看到以下网址:

FunctionName: [POST] http://localhost:7295/api/FunctionName

RenderOAuth2Redirect: [GET] http://localhost:7295/api/oauth2-redirect.html

RenderOpenApiDocument: [GET] http://localhost:7295/api/openapi/{version}.{extension}

RenderSwaggerDocument: [GET] http://localhost:7295/api/swagger.{extension}

RenderSwaggerUI: [GET] http://localhost:7295/api/swagger/ui

我的函数的 url 以 <base url>/api/FunctionName 开头,但是所有 OpenAPI 特定的 url 都位于 api 路由的根目录中,例如 <base url>/api/swagger/ui

如何更改 OpenAPI 默认 url,以便该 url 不会出现在 /api 路由下,但具有以下格式

<base url>/api/FunctionName/swagger/ui

在调查过程中,我想出了这个 Stack Overflow Answer 。不过,它展示了如何将 Swagger 默认值添加到 app 对象。我不知道如何在独立函数中获取 app 对象,在哪里可以添加 swagger 默认值?

最佳答案

How can I change the OpenAPI default url so that the the urls do not appear under /api route

您可以在 host.json 文件中添加 routePrefix

{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
},
"enableLiveMetricsFilters": true
}
},
"extensions": {
"http": {
"routePrefix": "api/{Your function name}"
}
}
}

输出:

enter image description here

.json 文件中的 BasePath 也发生了更改

enter image description here

关于azure - 更改 .NET 6 隔离函数的 OpenAPI 的默认 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76970271/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com