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

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.

主站蜘蛛池模板: 泰和县| 乌兰察布市| 滕州市| 清徐县| 富源县| 汉中市| 英山县| 莒南县| 苗栗市| 浦东新区| 进贤县| 安多县| 包头市| 汉沽区| 乐至县| 合山市| 金乡县| 芷江| 临安市| 杭锦旗| 阳东县| 突泉县| 陈巴尔虎旗| 棋牌| 达日县| 剑河县| 松滋市| 曲阳县| 鄂州市| 泗洪县| 平安县| 阳原县| 旅游| 彭山县| 屏边| 贵定县| 徐闻县| 栖霞市| 宁海县| 马山县| 奉新县|