【发布时间】:2022-02-21 14:40:04
【问题描述】:
我正在尝试按照Microsoft Docs here 生成我的 Swagger 文档
使用其他答案here 和here 在这个问题上没有取得任何进展...
我正在像这样配置 swagger gen:
serviceCollection.AddSwaggerGen(setupAction =>
{
setupAction.SwaggerDoc(
"Open API Specification for Haros v1",
new OpenApiInfo()
{
Title = "Haros API",
Version = "v1",
Description = "Haros API Description",
Contact = new OpenApiContact()
{
Email = "myname@domain",
Name = "My Name"
},
License = new OpenApiLicense()
{
Name = "MIT License"
}
});
setupAction.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.Http,
Scheme = "bearer",
BearerFormat = "JWT",
Description = "Attach your bearer token in this format to consume the API: Bearer {token}",
});
setupAction.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer",
},
}, new List<string>()
},
});
});
然后像这样添加中间件:
app.UseSwagger();
app.UseSwaggerUI(setupAction =>
{
setupAction.SwaggerEndpoint("/swagger/v1/swagger.json", "Haros API v1");
setupAction.RoutePrefix = string.Empty;
});
两个端点(https://localhost:5001/swagger/index.html 和 https://localhost:5001/swagger/v1/swagger.json)都返回 404。
我已将 GenerateDocumentationFile 选项添加到我的项目中,并使用 /// cmets 修饰了我的 API 控制器端点。
我注意到的是,如果我注释掉 RoutePrefix 我设法加载这个:
我尽可能地关注 Microsoft 文档链接,我没有看到任何我可能遗漏的内容。有谁知道我做错了什么或错过了什么?
(附加信息:使用 .NET 5 并通过 Kestrel 加载)
【问题讨论】:
-
试试这个:
setupAction.RoutePrefix = "swagger"; -
@segmentation_fault 打开控制台视图并在新选项卡中打开错误链接会显示一些详细的错误,
-
@SaeedEsmaeelinejad 将
RoutePrefix更改为您的建议会产生我在问题中附加的图像。 @ZahidMustafa 控制台除了请求 404 之外没有任何有用的信息。深入研究请求不会产生更多有用的信息
标签: c# asp.net-core swagger