Net 8 AP層的變數設定
載入變數的順序
當Asp.net core 服務開啟時,會有載入的順序,越晚載入的變數,會蓋掉之前設定的變數
appsettings.{Enviroment}.json -> Environment variables -> Command-line argument ->other sources
使用appsettings.{Enviroment}.json,設定應用程式變數
{ "AppConfig": { "SmptIP": "gmail.com.tw", "SmptPort": "512" } }
使用環境變數,設定應用程式變數
set AppConfig__SmptIP=smpt.google.com set AppConfig__SmptPort=433
使用Command-line argument,設定應用程式變數
F:\source\WebApplication1\WebApplication1\bin\>WebApplication1.exe --AppConfig:SmptIP="smpt.hotmail.com" --AppConfig:SmptPort=500
自我設定appsettings.json檔案路徑
Program.cs 加上AddJsonFile方式來讓保護環境參數檔案
builder.Configuration.AddJsonFile(@"E:\site.appsettings.json");
取得appsettings.{Enviroment}.json的方式
- (1) 設定建構子IConfiguration參數
public WeatherForecastController(IConfiguration configuration) { _configuration = configuration; }
- (2) 設定IConfiguration參數
新增類別:AppSettingOptions
public class AppSettingOptions { public const string Appsettings = "AppConfig"; public required string Site { get; set; } = "www.example.com"; public required int Port { get; set; } = 500; }
appsettings.json
{ "ConnectionStrings": {}, "AppConfig": { "Site": "www.google.com.tw", "Port": 90 }
WeatherForecastController.cs: 新增TestGet()方法
[HttpGet("TestGet", Name = "TestGet")] public string TestGet() { AppSettingOptions? appSettingOptions = _configuration.Get(); return $"Site:{appSettingOptions?.Site}, Port:{appSettingOptions?.Port}"; }
WeatherForecastController.cs: 新增TestGetValue()方法
[HttpGet("TestGetValue", Name = "TestGetValue")] public string TestGetValue() { string? Site = _configuration.GetValue("AppConfig:Site"); string? Port = _configuration.GetValue ("AppConfig:Port"); return $"Site:{Site}, Port:{Port}"; }
- (3) 設定環境變數的方式
WeatherForecastController.cs: 新增TestGetEnv()方法
public string TestGetEnv() { //第一種抓取環境變數的方法 string? Port2 = System.Environment.GetEnvironmentVariable("SmptPort2"); //第二種抓取環境變數的方法 string? Site = _configuration["SmptIP"]; string? Port = _configuration["SmptPort"]; return $"Site:{Site}, Port:{Port}, Port2:{Port2}"; }
結果:Site:gmail333.com.tw, Port:, Port2:test
取得appsettings.json 檢查變數去卡控
- AppSettingOptions :加上檢查機制
public class AppSettingOptions { public const string Appsettings = "AppConfig"; public required string Site { get; set; } = "www.example.com"; [RegularExpression("^\\d{4}$")] public required int Port { get; set; } ; }
- appsettings.json : 將port改成字串,後面希望啟動時可以檢查出錯誤
{ "AppConfig": { "Site": "www.google.com.tw", "Port": "test" } }
- Programs.cs
//註冊AppSettingOptions時,可以檢查屬性有沒有符合RegularExpression builder.Services.AddOptions() .Bind(builder.Configuration.GetSection(AppSettingOptions.Appsettings)) .ValidateDataAnnotations() .ValidateOnStart();
出現錯誤,訊息告訴你'AppConfig:Port' to type 'System.Int32,應該是整數
- WeatherForecastController.cs
private readonly ILogger_logger; private readonly IConfiguration _configuration; private readonly IOptions _options; public WeatherForecastController(ILogger logger, IConfiguration configuration, IOptions options) { _logger = logger; _configuration = configuration; this._options = options; } [HttpGet("TestGet", Name = "TestGet")] public string TestGet() { return $"Site:{_options?.Value.Site}, Port:{_options?.Value.Port}"; }
可以看到將Port 改成8080就成功
留言
張貼留言