asp.net core 获取 appsettings.json 配置
2017-06-19 16:12
836 查看
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");
"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 Web中使用appsettings.json配置文件的方法
- 【无私分享:ASP.NET CORE 项目实战(第六章)】读取配置文件(一) appsettings.json
- 【无私分享:ASP.NET CORE 项目实战(第六章)】读取配置文件(一) appsettings.json
- ASP.Net Core 2.0 获取Json和Xml格式的配置信息
- ASP.NET Core 注入和获取 AppSettings 配置
- ASP.NET Core读取appsettings.json的配置
- IT咨询顾问:一次吐血的项目救火 java或判断优化小技巧 asp.net core Session的测试使用心得 【.NET架构】BIM软件架构02:Web管控平台后台架构 NetCore入门篇:(十一)NetCore项目读取配置文件appsettings.json 使用LINQ生成Where的SQL语句 js_jquery_创建cookie有效期问题_时区问题
- asp.net core 读取appsettings.json配置项
- 2.1命令行和JSON的配置「深入浅出ASP.NET Core系列」
- 菜鸟入门【ASP.NET Core】5:命令行配置、Json文件配置、Bind读取配置到C#实例、在Core Mvc中使用Options
- 2.1命令行和JSON的配置「深入浅出ASP.NET Core系列」
- asp.net core 如何在Controller获取配置文件的值
- 详解ASP.NET Core 在 JSON 文件中配置依赖注入
- ASP.NET Core 在 JSON 文件中配置依赖注入
- 用"hosting.json"配置ASP.NET Core站点的Hosting环境
- 用"hosting.json"配置ASP.NET Core站点的Hosting环境
- [C#]使用 C# 代码实现拓扑排序 dotNet Core WEB程序使用 Nginx反向代理 C#里面获得应用程序的当前路径 关于Nginx设置端口号,在Asp.net 获取不到的,解决办法 .Net程序员 初学Ubuntu ,配置Nignix 夜深了,写了个JQuery的省市区三级级联效果
- 【ASP.NET Core快速入门】(五)命令行配置、Json文件配置、Bind读取配置到C#实例、在Core Mvc中使用Options
- ASP.NET CORE入门之读取Json配置文件