一、自定義讀取組態檔資訊appsettings.Json
1.在Startup類配置
1 public IConfiguration Configuration { get; } 2 public IWebHostEnvironment Env { get; } 3 public Startup(IConfiguration configuration, IWebHostEnvironment env) 4 { 5 Configuration = configuration; 6 Env = env; 7 }
2.在Startup類的ConfigureServices配置
services.AddSingleton(new Appsettings(Env.ContentRootPath));
3.新建一個appsettings操作類
static IConfiguration Configuration { get; set; } static string contentPath { get; set; } public Appsettings(string contentPath) { string Path = "appsettings.json"; Configuration = new ConfigurationBuilder() .SetBasePath(contentPath) .Add(new JsonConfigurationSource { Path = Path, Optional = false, ReloadOnChange = true }) .Build(); } /// <summary> /// 封裝要操作的字符 /// </summary> /// <param name="sections">節點配置</param> /// <returns></returns> public static string Get(params string[] sections) { try { if (sections.Any()) { return Configuration[string.Join(":", sections)]; } } catch (Exception) { } return ""; }
4.如何使用
string test = appsettings.Get(new string[] { "test", "test1" });


轉載請註明出處,本文鏈接:https://www.uj5u.com/net/42888.html
標籤:.NET Core
上一篇:.Net Core Cors跨域
下一篇:視頻圖文教學 - 用最快的速度把 DotNet Core Blazor 程式安裝到 樹莓派中 并且用網頁控制 GPIO 閃燈
