成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

跟著官網學ASP.NET Core 6.0之讀取配置文件

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

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

ASP.NET Core默認加載順序是appsettings.json->

appsettings.Environment.json,它會根據當前的運行環境去加載不同的配置文件,最后

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

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

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

簡單讀取配置

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

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"];

當然,它還可以更深的層級,如: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信息,

配置綁定到實體

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

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

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

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

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

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

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

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

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便會顯得雜亂臃腫,所以,我們可以將其移動到擴展方法以注冊服務,這里新建一個擴展類

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

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


責任編輯:武曉燕 來源: 今日頭條
相關推薦

2024-08-19 01:00:00

讀取配置文件接口應用程序

2024-03-15 11:35:11

配置文件應用程序開發

2018-08-20 08:03:46

跨平臺 Web操作系統

2009-07-21 10:05:10

ASP.NET配置文件

2009-08-05 10:57:17

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

2022-02-09 07:52:36

GolangGo語言

2021-02-19 06:54:33

配置系統ASP.NET Cor

2009-07-29 14:23:08

ASP.NET配置文件

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-09-09 07:37:51

AspJWT權限

2024-06-11 09:00:00

異步編程代碼

2009-08-13 15:49:18

ASP.NET性能優化

2009-08-10 13:32:15

ASP.NET TimASP.NET組件設計

2025-01-15 00:01:00

開發應用界面
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 午夜小视频在线观看 | 91精品亚洲 | 男女一区二区三区 | 嫩草视频在线 | 精品91久久| 中文字幕视频一区 | 中文字幕亚洲一区二区三区 | 最近中文字幕免费 | 亚洲免费精品 | 秋霞a级毛片在线看 | 中文字幕一区二区在线观看 | 中文字幕1区2区 | 国产精品美女久久久久久久久久久 | 日韩电影中文字幕在线观看 | 最近日韩中文字幕 | 久久爆操 | 99精品一区二区 | 成人精品视频99在线观看免费 | 日日夜夜精品免费视频 | 久久久www成人免费精品张筱雨 | 色播久久久 | 小h片免费观看久久久久 | 欧美日韩国产精品一区二区 | 狠狠影院| 能看的av网站 | 日韩成人在线视频 | 色www精品视频在线观看 | 一区观看 | 日韩一区二区在线免费观看 | 精品香蕉一区二区三区 | 自拍偷拍视频网 | 青青草一区 | 欧美精品在线免费观看 | 色影视 | 午夜寂寞影院在线观看 | 性高湖久久久久久久久3小时 | 精品一二区 | 91亚洲欧美 | 亚洲视频在线看 | 欧美性猛片aaaaaaa做受 | 欧美精品久久久久 |