自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開發(fā) 前端
在ASP.NET Core 6.0中,默認(rèn)配置文件是appsettings.json,該文件存儲的內(nèi)容為JSON格式的字符串,我們一般都將程序的配置放在這個文件里面,提供給程序使用,那么我們該如何操作呢?

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

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

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<WechatConfiguration>();

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

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

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

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

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

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

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

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

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

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);

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


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

2024-03-15 11:35:11

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

2024-08-19 01:00:00

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

2018-08-20 08:03:46

跨平臺 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語言

2009-07-29 14:23:08

ASP.NET配置文件

2021-02-19 06:54:33

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

2021-10-19 10:42:00

MVCAPI.NET

2011-04-19 14:35:58

ASP.NETWeb.config

2009-07-28 17:17:19

ASP.NET概述

2009-08-05 11:16:26

ASP.NET配置文件

2023-07-04 08:26:15

2025-04-18 08:45:26

2009-08-13 15:49:18

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

2024-09-09 07:37:51

AspJWT權(quán)限

2024-06-11 09:00:00

異步編程代碼

2009-08-10 13:32:15

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

2024-10-21 07:15:08

2025-01-15 00:01:00

開發(fā)應(yīng)用界面
點贊
收藏

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