在我的Swagger页面上得到常见的“响应状态是404 https://localhost:7247/swagger/v1/swagger.json”错误。
如果我手动打开并访问页面,我不会得到任何ASP.NET错误,我也只会得到一个404响应。
此问题的大多数示例都在控制台中显示错误,可以解决这些错误以修复生成的swagger.json
的缺失。
在我的例子中,当我运行我的ASP.NETCore应用程序时,终端中没有显示任何错误。
运行.NET 7.0,我的大多数nuget包都是7.0.12版本
我只定义了2个控制器,通过编译设置禁用这两个控制器并不能解决这个问题,所以我不相信这是控制器路由配置错误导致的问题。
我的Program.cs
如下:
public static async Task Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
var config = EnsureConfig(builder);
builder.Services.AddDbContext<NotesDbContext>(options =>
{
options.UseNpgsql(config.Database.ConnectionString);
});
EnsureIdentity(builder, config);
builder.Services.AddControllers();
if (builder.Environment.IsDevelopment())
{
EnsureSwagger(builder); //Breakpoint here does get hit
}
builder.Services.AddBusinessServices();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var dbInitService = scope.ServiceProvider.GetRequiredService<IDatabaseInitService>();
await dbInitService.InitAsync();
}
app.UseHttpsRedirection();
if (app.Environment.IsDevelopment())
{
app.UseSwagger(); //Breakpoint here does get hit
app.UseSwaggerUI();
}
app.UseAuthentication();
app.UseAuthorization();
app.UseStaticFiles();
app.MapControllers();
app.MapFallbackToFile("index.html");
await app.RunAsync();
}
private static NotesConfig EnsureConfig(WebApplicationBuilder builder)
{
var config = builder.Configuration.Get<NotesConfig>() ??
throw new ConfigurationErrorsException(
"Invalid Configuration, please validate configuration integrity"
);
config.Validate();
builder.Services.AddSingleton(config);
return config;
}
private static void EnsureSwagger(WebApplicationBuilder builder)
{
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("Notes", new()
{
Description = "Open API Docs for Notes v1",
Title = "Notes",
Version = "v1",
Contact = new()
{
Email = "st[email protected]",
Name = "Steffen Blake",
Url = new Uri("https://technically.fun"),
}
});
options.AddSecurityDefinition("Bearer", new()
{
In = ParameterLocation.Header,
Description = "Please insert JWT with Bearer into field",
BearerFormat = "Bearer <token>",
Name = "Authorization",
Type = SecuritySchemeType.ApiKey
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
// scopes
new string[] { }
}
});
});
}
private static void EnsureIdentity(WebApplicationBuilder builder, NotesConfig config)
{
builder.Services
.AddDefaultIdentity<NotesUser>(options =>
{
options.Lockout = config.Identity.Lockout;
options.Password = config.Identity.Password;
options.SignIn = config.Identity.SignIn;
options.User = config.Identity.User;
})
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<NotesDbContext>();
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
var key = Encoding.UTF8.GetBytes(config.Signing.JWTSecret);
options.TokenValidationParameters = new()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = NotesConstants.JWT_ISSUER,
ValidAudience = NotesConstants.JWT_ISSUER,
IssuerSigningKey = new SymmetricSecurityKey(key)
};
});
}
字符串
我猜这是一个“你在你的服务或应用程序上声明的注册顺序稍微错误,所以它默默地失败了”的场景,所以我一直在翻转声明的顺序,看看它是否补救了这种情况,但到目前为止还没有骰子。
也有可能是我的swagger配置的某个部分是错误的,我猜,导致它默默地失败了?
1条答案
按热度按时间enxuqcxy1#
找到了具体的案例,和这里的评论一样:https://stackoverflow.com/a/53521371/7839602
修正了Swagger gen部分添加了以下内容:
字符串
重要的是
options.SwaggerDoc(...)
中的第一个参数必须与options.SwaggerEndpoint(...)
的第二个url片段相同。在我的例子中,以
Notes
为例