【发布时间】:2021-12-04 03:25:56
【问题描述】:
我有一个带有 API 版本控制和招摇的 .NET 5.0 API 项目设置。这是我的ConfigureServices 方法:
services.AddSwaggerGen(c => {
// Set the swagger doc stub
c.SwaggerDoc("v1", new OpenApiInfo {
Version = "v1",
Title = "API"
});
// Set the comments path for the Swagger JSON and UI.
string xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
string xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
这是我的Configure 方法:
app.UseSwagger();
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1");
c.RoutePrefix = string.Empty;
});
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
});
我遇到的问题是,当我在本地运行项目时,/swagger 端点返回 404。但是,导航到 /swagger/v1/swagger.json 会返回 swagger JSON 文档。我见过类似的问题here、here 和here,但是这里提出的解决方案都没有解决这个问题,主要是因为我没有使用 IIS。我还查看了 Microsoft 的官方文档 here,但没有发现任何差异。我在这里做错了什么?
【问题讨论】:
-
你能在 github 上与我们分享一个 hello world 吗?我在 netcore 3.1 中使用 swagger 没有任何问题
-
@JRichardsz 我不知道你在问什么
-
stackoverflow.com/help/how-to-ask 部分:帮助重现错误。如果您在另一个最小项目中重现该错误,其他人可以运行您的代码并向您显示错误。有时在重现错误的过程中,您可以自己发现错误。 Swagger 不需要数据库,因此很容易配置。我确定这是一些设置或中间件