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

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.

主站蜘蛛池模板: 普兰店市| 乐安县| 三亚市| 广宗县| 麟游县| 乐昌市| 缙云县| 玉田县| 邹平县| 湖北省| 岑溪市| 治县。| 龙州县| 库伦旗| 齐齐哈尔市| 南漳县| 云龙县| 鞍山市| 萨嘎县| 郴州市| 五指山市| 社旗县| 平昌县| 沾益县| 昭通市| 甘洛县| 互助| 新疆| 北京市| 莲花县| 驻马店市| 灯塔市| 邻水| 万州区| 吴桥县| 斗六市| 哈密市| 连江县| 阳西县| 博湖县| 宁晋县|