偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

跟著官網(wǎng)學(xué)ASP.NET Core 6.0之讀取配置文件

開(kāi)發(fā) 前端
在ASP.NET Core 6.0中,默認(rèn)配置文件是appsettings.json,該文件存儲(chǔ)的內(nèi)容為JSON格式的字符串,我們一般都將程序的配置放在這個(gè)文件里面,提供給程序使用,那么我們?cè)撊绾尾僮髂兀?/div>

在ASP.NET Core 6.0中,默認(rèn)配置文件是appsettings.json,該文件存儲(chǔ)的內(nèi)容為JSON格式的字符串,我們一般都將程序的配置放在這個(gè)文件里面,提供給程序使用,那么我們?cè)撊绾尾僮髂?

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

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

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

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

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

簡(jiǎn)單讀取配置

現(xiàn)在我們就嘗試讀取配置文件中AppId和AppSecret的值,在Program.cs中,我們直接可以用WebApplicationBuilder里面的Configuration屬性來(lái)讀取,取配置內(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)然,它還可以更深的層級(jí),如:builder.Configuration["

AppConfig:Wechat:AppSecret"]

如果我們想在非Program.cs里面讀取配置,則需要注入IConfiguration實(shí)例,其他操作方式便和前面的一致,我們還在來(lái)實(shí)踐一次,這里我先新建一個(gè)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"];
}
}

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

配置綁定到實(shí)體

如果配置文件比較復(fù)雜,我們依然用前面的方式一個(gè)個(gè)的去取值,那確實(shí)有些繁瑣,所以,我們需要更高端的操作,直接把配置內(nèi)容裝載到實(shí)體類中,這我新建一個(gè)名為WechatConfiguration的類,里面加入與配置文件對(duì)應(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方法來(lái)獲取指定節(jié)點(diǎn)的內(nèi)容,然后使用Get將內(nèi)容序列化為對(duì)象,看例子

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

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

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

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

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

然后在需要使用的類中注入IOptions,通過(guò)其Value屬性來(lái)獲取配置類對(duì)象

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

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

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

ConfigServiceCollectionExtensions,將前面的代碼移動(dòng)到該類中

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

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

builder.Services.AddConfig(builder.Configuration);

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


責(zé)任編輯:武曉燕 來(lái)源: 今日頭條
相關(guān)推薦

2024-08-19 01:00:00

讀取配置文件接口應(yīng)用程序

2024-03-15 11:35:11

配置文件應(yīng)用程序開(kāi)發(fā)

2018-08-20 08:03:46

跨平臺(tái) Web操作系統(tǒng)

2009-07-21 10:05:10

ASP.NET配置文件

2009-08-05 10:57:17

ASP.NET配置文件配置文件格式

2022-02-09 07:52:36

GolangGo語(yǔ)言

2009-07-29 14:23:08

ASP.NET配置文件

2021-02-19 06:54:33

配置系統(tǒng)ASP.NET Cor

2023-07-04 08:26:15

2009-07-28 17:17:19

ASP.NET概述

2021-10-19 10:42:00

MVCAPI.NET

2009-08-05 11:16:26

ASP.NET配置文件

2011-04-19 14:35:58

ASP.NETWeb.config

2024-10-21 07:15:08

2025-04-18 08:45:26

2024-06-11 09:00:00

異步編程代碼

2024-09-09 07:37:51

AspJWT權(quán)限

2009-08-13 15:49:18

ASP.NET性能優(yōu)化

2009-08-10 13:32:15

ASP.NET TimASP.NET組件設(shè)計(jì)

2025-01-15 00:01:00

開(kāi)發(fā)應(yīng)用界面
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)