.net core 读取 appsetting.json
文件 appsetting.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "User ID=sa;Password=123@abcd;Initial Catalog=CommonPower;Data Source=AFOCL-703281007\\SQLEXPRESS",
"ProviderName": "SqlServer" //驱动提供者(数据库品牌) //sqlserver、oracle、mysql
},
//"AppSettings": {
// "ConnectionString": "Server=10.0.0.1;database=HasngCadreFile;uid=sa;pwd=123-abc", //数据库连接字符串
// //"ConnectionString": "Server=192.168.1.250;database=HasngCadreFile;uid=sa;pwd=123-abc", //数据库连接字符串
// "ProviderName": "SqlServer" //驱动提供者(数据库品牌) //sqlserver、oracle、mysql
//},
"RedisConfig": {
"Connection": "10.0.0.1:6379,abortConnect=false,password=123456"
//"Connection": "192.168.1.250:6379,abortConnect=false,password=123456"
},
"ServiceAddress": {
"Address": "http://10.0.0.1:5052",
//"Address": "http://192.168.1.250:5052",
"IsOpenOnlyUser": false, //一个账号多地点登陆 true,false
"PageTimeOut": 20 //数字 (分钟)
}
}
2.读取配置文件方式
DbHelper.ConnectionString = Configuration.GetConnectionString("DefaultConnection"); DbHelper.DatabaseTypeEnumParse(Configuration.GetConnectionString("ProviderName")); //DbHelper.ConnectionString = Configuration.GetSection("AppSettings").GetSection("ConnectionString").Value; //DbHelper.DatabaseTypeEnumParse(Configuration.GetSection("AppSettings").GetSection("ProviderName").Value); DbHelper.DbParmChar = DbFactory.CreateDbParmCharacter(); //配置缓存信息 CacheHelper.ConnectionString = Configuration.GetSection("RedisConfig").GetSection("Connection").Value; //读取守护程序地址 GlobalStatus.Address = Configuration.GetSection("ServiceAddress").GetSection("Address").Value; //读取页面过期时间(分钟) GlobalStatus.PageTimeOut = Convert.ToInt32(Configuration.GetSection("ServiceAddress").GetSection("PageTimeOut").Value); //读取是否开启单用户登陆认证 GlobalStatus.IsOpenOnlyUser = Convert.ToBoolean(Configuration.GetSection("ServiceAddress").GetSection("IsOpenOnlyUser").Value);
3.对象方式读取配置文件节点
public class AppSettings { /// <summary> /// 连接字符串 /// </summary> public string ConnectionString { get; set; } /// <summary> /// 驱动提供者 /// </summary> public string ProviderName { get; set; } public string Connection { get; set; } public string Address { get; set; } public bool IsOpenOnlyUser { get; set; } public string PageTimeOut { get; set; } }
startup.cs
public void ConfigureServices(IServiceCollection services) { services.AddSingleton(Configuration);
//关键点,配置映射文件
services.Configure<AppSettings>(Configuration.GetSection("AppSettings")); services.AddMvc();
}
controller.cs
public class AccountController : Controller { AppSettings _op; public AccountController(IOptions<AppSettings> op) { _op = op.Value; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2020-06-29 JSON JavaScriptSerializer 字符串的长度超过了为 maxJsonLength 属性设置的值。
2018-06-29 sql左右连接测试
2017-06-29 设计模式---装饰模式(Decorator)
2017-06-29 设计模式---订阅发布模式(Subscribe/Publish)