我试图在我的 Asp.net 核心 v6应用程序 Program.cs 文件中访问 appsetings.json,但是在这个版本的。Net Startup 类和 Program 类合并在一起,using 语句和其他语句被简化并从 Program.cs 中删除。在这种情况下,如何访问 iConfiguration 或如何使用依赖注入?
这是 Asp.net 6为我创建的默认 Program.cs
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddStackExchangeRedisCache(options =>
{
options.Configuration = "localhost:6379";
});
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new() { Title = "BasketAPI", Version = "v1" });
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "BasketAPI v1"));
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
例如,我想在这一行中使用 appsetings.json 而不是硬类型的 Connectionstring:
options.Configuration = "localhost:6379";