前言
方法很多,下面的例子也是從百度上搜索到的,原文鏈接已經找不到了。
方法1
1.添加NovelSetting節點,寫入相關的配置信息
2.創建類,字段與上面的配置一致
3.StartUp.cs中獲取
添加讀取的方法,將配置信息寫入到對應的參數類中
4.使用
在控制器中,如下設置,即可。
方法2
1.在appsetting.json中創建節點,保存相關的配置
2.創建操作類
using Microsoft.Extensions.Configuration; namespace NoteServer.Core { /// <summary> /// 配置信息 /// </summary> public class AppSettings { private static IConfigurationSection appSection = null; /// <summary> /// 獲取配置文件 /// </summary> /// <param name="key"></param> /// <returns></returns> public static string GetAppSeting(string key) { if (appSection.GetSection(key) != null) { return appSection.GetSection(key).Value; } else { return ""; } } /// <summary> /// 設置配置文件 /// </summary> /// <param name="section"></param> public static void SetAppSetting(IConfigurationSection section) { appSection = section; } } }
3.在StartUp.cs中獲取
4.使用