日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
跟著官網(wǎng)學(xué)ASP.NETCore6.0之讀取配置文件

ASP.NET Core默認(rèn)加載順序是appsettings.json->

創(chuàng)新互聯(lián)不只是一家網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司;我們對營銷、技術(shù)、服務(wù)都有自己獨特見解,公司采取“創(chuàng)意+綜合+營銷”一體化的方式為您提供更專業(yè)的服務(wù)!我們經(jīng)歷的每一步也許不一定是最完美的,但每一步都有值得深思的意義。我們珍視每一份信任,關(guān)注我們的網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計質(zhì)量和服務(wù)品質(zhì),在得到用戶滿意的同時,也能得到同行業(yè)的專業(yè)認(rèn)可,能夠為行業(yè)創(chuàng)新發(fā)展助力。未來將繼續(xù)專注于技術(shù)創(chuàng)新,服務(wù)升級,滿足企業(yè)一站式全網(wǎng)整合營銷推廣需求,讓再小的高端網(wǎng)站設(shè)計也能產(chǎn)生價值!

appsettings.Environment.json,它會根據(jù)當(dāng)前的運行環(huán)境去加載不同的配置文件,最后

appsettings.Environment.json 值將替代 appsettings.json 中的值,如果沒有多個值,則取默認(rèn)值。

在開始之前,我們先在appsettings.json中新增一些配置信息

"Wechat": {
"AppId": "wx26c607c55f31745e",
"AppSecret": "e7da82499266ca3fdf85290f68f8fd3a"
}

簡單讀取配置

現(xiàn)在我們就嘗試讀取配置文件中AppId和AppSecret的值,在Program.cs中,我們直接可以用WebApplicationBuilder里面的Configuration屬性來讀取,取配置內(nèi)容的方式有很多,比如:

string appId = builder.Configuration.GetSection("Wechat")["AppId"];
string appSecret = builder.Configuration.GetSection("Wechat")["AppSecret"];

還可以這樣

string appId1 = builder.Configuration["Wechat:AppId"];
string appSecret1 = builder.Configuration["Wechat:AppSecret"];

當(dāng)然,它還可以更深的層級,如:builder.Configuration["

AppConfig:Wechat:AppSecret"]

如果我們想在非Program.cs里面讀取配置,則需要注入IConfiguration實例,其他操作方式便和前面的一致,我們還在來實踐一次,這里我先新建一個Controller

[Route("api/[controller]")]
[ApiController]
public class ConfigurationController : ControllerBase
{
private readonly IConfiguration Configuration;

public ConfigurationController(IConfiguration configuration) {
Configuration = configuration;
}

[HttpGet]
public string ReadConfig()
{
return Configuration["Wechat:AppId"];
}
}

我們直接訪問api/Configuration,便能返回配置文件中的AppId信息,

配置綁定到實體

如果配置文件比較復(fù)雜,我們依然用前面的方式一個個的去取值,那確實有些繁瑣,所以,我們需要更高端的操作,直接把配置內(nèi)容裝載到實體類中,這我新建一個名為WechatConfiguration的類,里面加入與配置文件對應(yīng)的屬性

public class WechatConfiguration
{
public const string KEY = "Wechat";

public string AppId { get; set; } = String.Empty;
public string AppSecret { get; set; } = String.Empty;
}

這里,我們需要使用的IConfiguration的GetSection方法來獲取指定節(jié)點的內(nèi)容,然后使用Get將內(nèi)容序列化為對象,看例子

Configuration.GetSection(WechatConfiguration.KEY).Get();

除了使用Get取值,還可使用Bind方法,將值綁定到對象上

WechatConfiguration wechatConfiguration = new WechatConfiguration();
Configuration.GetSection(WechatConfiguration.KEY).Bind(wechatConfiguration);

這兩種方式都能獲取到配置文件修改后的內(nèi)容,除了上面兩種方式獲取配置內(nèi)容外,還可以使用直接將配置對象注入到容器中,

builder.Services.Configure(builder.Configuration.GetSection(WechatConfiguration.KEY));

然后在需要使用的類中注入IOptions,通過其Value屬性來獲取配置類對象

public class ConfigurationController : ControllerBase
{
private readonly IConfiguration Configuration;
private readonly WechatConfiguration wechat;

public ConfigurationController(IConfiguration configuration, IOptions options)
{
Configuration = configuration;
wechat = options.Value;
}
}

如果配置類過多,那么在Program.cs便會顯得雜亂臃腫,所以,我們可以將其移動到擴(kuò)展方法以注冊服務(wù),這里新建一個擴(kuò)展類

ConfigServiceCollectionExtensions,將前面的代碼移動到該類中

public static class ConfigServiceCollectionExtensions
{
public static IServiceCollection AddConfig(this IServiceCollection services, IConfiguration config)
{
services.Configure(config.GetSection(WechatConfiguration.KEY));
return services;
}
}

然后在Program.cs中添加引用即可

builder.Services.AddConfig(builder.Configuration);

配置文件讀取就先了解這么,明天就要開始上班了,又要忙起來了,后面有時間再繼續(xù)學(xué)習(xí)ASP.NET Core 中的路由。


分享名稱:跟著官網(wǎng)學(xué)ASP.NETCore6.0之讀取配置文件
文章出自:http://www.5511xx.com/article/djocjsp.html