gpt4 book ai didi

c# - 在 Azure 中的 ASP.NET Core 3.1 应用程序中获取 swagger-ui 返回 404

转载 作者:行者123 更新时间:2023-12-04 13:28:40 26 4
gpt4 key购买 nike

加载/swagger/index.html 页面时,浏览器找不到部署到 Azure 中的应用程序服务所需的 swagger-ui 资源,返回 404。它在本地运行时有效。我的设置是:

  services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Nexus WebApp",
Version = "v1"
});
options.CustomSchemaIds(type => type.ToString());

});
 var builder = endpoints.CreateApplicationBuilder();

builder.UseSwagger();

builder.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Nexus WebApp");
});

var pipeline = builder.Build();

endpoints.Map("/swagger", pipeline)
.RequireAuthorization(new AuthorizeAttribute());
endpoints.Map("/swagger/index.html", pipeline)
.RequireAuthorization(new AuthorizeAttribute());
endpoints.Map("/swagger/{documentName}/swagger.json", pipeline)
.RequireAuthorization(new AuthorizeAttribute());

我尝试过使用 swagger 端点的相对路径,例如 ..\swagger\v1/swagger.json,不幸的是,我尝试指定 RoutePrefix 都无济于事。我知道有人问过类似的问题,但不幸的是似乎没有一个有帮助。

有人有线索吗?

更新

这些是它返回 404 的资源:

  • https://{domain}/swagger/swagger-ui.css
  • https://{domain}/swagger/swagger-ui-standalone-preset.js
  • https://{domain}/swagger/swagger-ui-bundle.js

最佳答案

我怀疑,你的实现是错误的。以下是在 NET Core 中设置 Swagger UI 的完整启动代码:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Swashbuckle.AspNetCore.Swagger;
namespace IDGSwaggerDemo
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion
(CompatibilityVersion.Version_2_2);
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
Version = "v1",
Title = "Swagger Demo",
Description = "Swagger Demo for ValuesController",
TermsOfService = "None",
Contact = new Contact() { Name = "Joydip Kanjilal",
Email = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97fdf8eef3fee7fcf6f9fdfefbf6fbd7eef6fff8f8b9f4f8fa" rel="noreferrer noopener nofollow">[email protected]</a>",
Url = "www.google.com"
}
});
});
}
public void Configure(IApplicationBuilder app,
IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
});
}
}
}

查看this有关更增强的 Swagger 实现的教程。

关于c# - 在 Azure 中的 ASP.NET Core 3.1 应用程序中获取 swagger-ui 返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66579845/

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