【发布时间】:2023-03-14 05:40:01
【问题描述】:
目前我正在使用 SwaggerUI 开发 .Net Core 3.1 服务。实际上我在 Swagger UI 中遇到了错误:
当我查看浏览器的 DevTools 时,我发现 SwaggerUI 进行了以下错误调用:
背景是:我的服务作为 IIS 上的子页面托管。因此我将 Swagger 配置为如下(Startup.cs):
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseHttpsRedirection()
.UseRouting()
.UseAuthentication()
.UseAuthorization()
.UseEndpoints(endpoints => endpoints.MapControllers())
.UseSwagger()
.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("./swagger/v1/swagger.json", ServiceConstants.ServiceDisplayName);
c.RoutePrefix = string.Empty;
});
}
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(IISDefaults.AuthenticationScheme);
services.Configure<IISOptions>(options =>
{
options.AutomaticAuthentication = true;
options.ForwardClientCertificate = true;
});
services.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApiInfo { Title = ServiceConstants.ServiceDisplayName, Description = ServiceConstants.ServiceDescription, Version = "v1" }));
}
我已经尝试过不使用相对路径./swagger/v1/swagger.json,但这仅适用于本地。我 .NetCore 2.1 我从来没有遇到过这样的问题。
我的控制器的一个例子是这样的:
[Authorize, Route(nameof(AdminController))]
public class AdminController : Controller
{
[HttpPost, Route(nameof(ClearAllCaches))]
public void ClearAllCaches()
{
// Do Something
}
}
我已经阅读了很多可能的解决方案,但它们都不适用于 .NetCore 3.1。我正在为 Swagger 使用以下 NugetPackages:
Swagger 本身工作正常,但错误有点烦人。有人有线索吗?
非常感谢!
【问题讨论】:
标签: c# iis .net-core swagger swagger-ui