asp.net core 获取 appsettings.json 配置
来源:互联网 发布:网络理财app 编辑:程序博客网 时间:2024/06/05 06:43
1,在 appsettings.json 中添加配置如:
"AppSetting": {
"connString": "Database=student_db;Data Source=localhost;User Id=root;Password=root;CharSet=utf8;port=3306"
}
2,建立存储 配置和读取配置的类
public class AppSettings
{
private static IConfigurationSection appSections = null;
public static string AppSetting(string key)
{
string str = "";
if (appSections.GetSection(key) != null)
{
str = appSections.GetSection(key).Value;
}
return str;
}
public static void SetAppSetting(IConfigurationSection section)
{
appSections = section;
}
}
3,在 站点 下的 Startup.cs 文件下 的 Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 方法中
调用 SetAppSetting 方法 设置 配置参数及其配置值 如:
Common.AppSettings.SetAppSetting(Configuration.GetSection("AppSetting"));
此处的配置节点 (AppSetting)要和 appsettings.json 中的节点一致
4,调用 AppSetting(string key) 方法 获取 配置参数
如 : private string connString =ConfigCommon.AppSettings.AppSetting("connString");
- asp.net core 获取 appsettings.json 配置
- ASP.NET Core读取appsettings.json的配置
- ASP.NET Core读取appsettings.json的配置
- asp.net core 读取Appsettings.json 配置文件
- .NET Core类库项目中如何读取appsettings.json中的配置
- Asp.NET Core 在Controller调用appsettings里的值
- Asp.Net Core-配置
- .NET Core 控制台程序读 appsettings.json 、注依赖、配日志、设 IOptions
- Asp.Net Core-Identity 配置
- ASP.NET Core 配置身份验证
- asp.net core json 大小写问题
- asp.net读取Web.config的<appSettings></appSettings>
- 用"hosting.json"配置ASP.NET Core站点的Hosting环境
- ASP.NET Core中配置CORS
- ASP.NET Core 2.0 全局配置项
- .net 中的哪些配置问题 appSettings
- ASP.NET CORE入门之读取Json配置文件
- Asp.net下获取JSON数据代码
- static关键字作用分析
- Python安装
- Js时间格式化
- 团队合作
- 微信APP支付工具类
- asp.net core 获取 appsettings.json 配置
- 关于切线空间的一些事
- EJB学习笔记-6-传值和传址
- ecshop的模板函数
- IntelliJ IDEA开发SpringMVC网站(下)
- Android获取手机设备信息
- JavaScript RegExp 对象
- discuz (dzx) FAQ帮助页面 图文并茂版
- laravel使用记录