如何在Asp core 6 Program.cs文件中使用appsettings.json
示例代码
从『管理NuGet程序包』的『浏览』中下载以下组件:
Microsoft.Extensions.Caching.StackExchangeRedis
(不要下载Microsoft.Extensions.Caching.Memory)
Swashbuckle.AspNetCore.SwaggerGen
(不要下载Swashbuckle.AspNetCore.Swagger)
Swashbuckle.AspNetCore.SwaggerUI-4.14.0
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddStackExchangeRedisCache(options =>
{
asp查看源码配置uioptions.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();
}
}
例如,想在这一行中使用 appsettings.json 而不是硬编码的连接字符串:
options.Configuration = "localhost:6379";
执行此操作的方法如下:
builder.Services.AddStackExchangeRedisCache(options =>
{
options.Configuration = builder.Configuration["Redis"];
});
WebApplicationBuilder有一个配置对象作为您可以使用的属性。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论