示例
新建一个ASP.NET Core Empty项目,项目名称ConfigurationExample
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"MyKey": "MyValue from appsettings.json updated"
}
Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
var app = builder.Build();
//app.MapGet("/", () => "Hello World!");
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.Map("/", async context =>
{
await context.Response.WriteAsync(app.Configuration["MyKey"] + "\n");
await context.Response.WriteAsync(app.Configuration.GetValue<string>("MyKey") + "\n");
await context.Response.WriteAsync(app.Configuration.GetValue<string>("MyKey1","Default") + "\n");
});
});
app.MapControllers();
app.Run();
Gitee获取源码: