官术网_书友最值得收藏!

Changes from version 2.x

The big change from version 2.0 was that, as of 2.1, the configuration is done by convention—that is, the process of adding appsettings.json JSON files (generic and optional per environment) and all that is hidden from the users.

This is defined in the WebHost.CreateDefaultBuilder method. You can, however, still build your own ConfigurationBuilder and add whatever you like to it. To do this, you call the ConfigureAppConfiguration method, as described in Chapter 1, Getting Started with ASP.NET Core, and illustrated in the following code block:

Host    .CreateDefaultBuilder(args)
    .ConfigureAppConfiguration(builder =>
    {
var jsonSource = new JsonConfigurationSource { Path =
"appsettings.json" }; builder.Add(jsonSource);
})
.ConfigureWebHostDefaults(builder =>
{
builder.UseStartup<Startup>();
});

Or, if you just want to add a single entry to the configuration that is built by default (or, to the one you're modifying), you call the UseSettings extension method, as follows:

Host    .CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(builder =>
{
builder.UseSetting("key", "value");
builder.UseStartup<Startup>();
});

So, when the Startup class is instantiated, it will get passed an IConfiguration object that is built from the code that you put in here.

Warning: when using UseSetting, the value will be written to all registered configuration providers.

After seeing how the application configuration is done, let's see how we can do the same for the host.

主站蜘蛛池模板: 通州市| 裕民县| 正定县| 泰兴市| 梁河县| 宁阳县| 南溪县| 灵武市| 巫山县| 建平县| 花莲市| 璧山县| 德令哈市| 永泰县| 密云县| 依安县| 虎林市| 江安县| 丰台区| 韶山市| 桐梓县| 曲麻莱县| 荔波县| 九江县| 陈巴尔虎旗| 左贡县| 曲阳县| 瑞安市| 盘锦市| 涟源市| 吴忠市| 淄博市| 洞口县| 民勤县| 贡嘎县| 古丈县| 霍州市| 甘德县| 武陟县| 琼结县| 炎陵县|