.net core 根据环境变量区分正式、测试 配置文件
新建测试、正式环境下所用的 配置信息文件

appsettings.Development.json 文件信息:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"db": {
"mysql": {
"conStr": "xxxxxxxxxxxxxxxxx", //测试
"providerName": "MySql.Data.MySqlClient"
},
"redis": {
"conStr": "xxxxxxxxxxxxxxxxx", //测试
"dbId": "3"
}
},
"ThOcUrl": "xxxxxxxxxxxxxxxxx", //测试
"ThSftpInfo": "xxxxxxxxxxxxxxxxx", //测试
"ThFileUrl": "xxxxxxxxxxxxxxxxx" //测试
}
加入相关nuget引用

添加获取配置信息帮助类
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
using Microsoft.Extensions.DependencyInjection; namespace YanWenCore.ThregGoods.Common
{
public static class MyServiceProvider
{
public static IServiceProvider ServiceProvider
{
get; set;
}
} public class AppSettingHelper
{
//根据环境变量配置获取json文件的名称
private static readonly string AppSettingName = String.Format("appsettings.{0}.json", MyServiceProvider.ServiceProvider.GetRequiredService<IHostingEnvironment>().EnvironmentName); private static IConfigurationRoot configR;
static AppSettingHelper()
{
var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile(AppSettingName);
configR = builder.Build();
} public static IConfigurationRoot GetRotConfig()
{
if (configR == null)
{
configR = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile(AppSettingName).Build();
}
return configR;
} //Mysql连接
public static string MysqlConnectionStringSettings
{
get
{
return GetRotConfig().GetSection("db").GetSection("mysql").GetSection("conStr").Value;
}
} //Redis连接信息
public static string RedisConnectionStringSettings
{
get
{
return GetRotConfig().GetSection("db").GetSection("redis").GetSection("conStr").Value;
}
} //OC平台接口地址
public static string GetbatteryCertUrl
{
get
{
return GetRotConfig().GetSection("ThOcUrl").Value;
}
} //sftp服务器信息
public static string ThSftpInfo
{
get
{
return GetRotConfig().GetSection("ThSftpInfo").Value;
}
} //文件服务器地址
public static string ThFileUrl
{
get
{
return GetRotConfig().GetSection("ThFileUrl").Value;
}
} } }
应用程序期间提供初始化提供服务容器的对象
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{ app.UseExceptionHandler("/Error");
}
MyServiceProvider.ServiceProvider = app.ApplicationServices;
}
执行docker run 之前,设置环境变量
set ASPNETCORE_ENVIRONMENT= Development
.net core 根据环境变量区分正式、测试 配置文件的更多相关文章
- JAVA_SE基础——2.环境变量的配置&测试JDK
哈喽,利用晚上的空余时间再写篇心的~~~ 谢谢大家 前一篇文章 JAVA_SE基础--JDK&JRE下载及安装http://blog.csdn.net/thescript_j/article ...
- ASP.NET Core使用环境变量
前言 通常在应用程序开发到正式上线,在这个过程中我们会分为多个阶段,通常会有 开发.测试.以及正式环境等.每个环境的参数配置我们会使用不同的参数,因此呢,在ASP.NET Core中就提供了相关的环境 ...
- 在ASP.NET Core配置环境变量和启动设置
在这一部分内容中,我们来讨论ASP.NET Core中的一个新功能:环境变量和启动设置,它将开发过程中的调试和测试变的更加简单.我们只需要简单的修改配置文件,就可以实现开发.预演.生产环境的切换. A ...
- ASP .NET CORE 根据环境变量支持多个 appsettings.json
0.背景 在开发项目的过程当中,生产环境与调试环境的配置肯定是不一样的.拿个最简单的例子来说,比如连接字符串这种东西,调试环境肯定是不能连接生产数据库的.在之前的话,这种情况只能说是你 COPY 两个 ...
- ASP.NET Core配置环境变量和启动设置
在这一部分内容中,我们来讨论ASP.NET Core中的一个新功能:环境变量和启动设置,它将开发过程中的调试和测试变的更加简单.我们只需要简单的修改配置文件,就可以实现开发.预演.生产环境的切换. A ...
- [转]ASP.NET Core配置环境变量和启动设置
本文转自:https://www.cnblogs.com/tdfblog/p/Environments-LaunchSettings-in-Asp-Net-Core.html 在这一部分内容中,我们来 ...
- .net core 中环境变量的配置
配置文件: Properties目录下的launchSettings.json IISExpress配置 "ASPNET_ENV": "EnvironmentOne&qu ...
- Web开发之tomcat配置及使用(环境变量设置及测试,一个简单的web应用实例)
Tomcat的配置及测试: 第一步:下载tomcat,然后解压到任意盘符 第二步:配置系统环境变量 tomcat解压到的D盘 (路径为: D:\tomcat), 配置环境变量: 启动tomcat需要两 ...
- [.net core] 12.环境变量
异常给开发人员提供了很大好的排错功能. 但是其背后的技术细节很复杂,会损耗性能,也会使.net core web app更容易被反派攻击. 于是我们要学会使用环境变量, 通过环境变量,控制一些逻辑 当 ...
随机推荐
- Linux NIO 系列(02) 阻塞式 IO
目录 一.环境准备 1.1 代码演示 二.Socket 是什么 2.1 socket 套接字 2.2 套接字描述符 2.3 文件描述符和文件指针的区别 三.基本的 SOCKET 接口函数 3.1 so ...
- 马士兵对话京东T6阿里P7(薪水):月薪5万,他为何要离职?
马士兵大佬你知道吗? 你竟然不知道?你怎么可能不知道!你不知道是不可能的! 记得自己的第一行Java代码,你的Hello World是跟着谁学的吗?我的就是马士兵老师! 马士兵是唯一一个在当时讲课是让 ...
- adb基本操作
用途 命令 备注 安装app adb install xxx.apk -l 锁定该应用程序-r 替换已存在的应用程序,也就是说强制安装-t 允许测试包-s 把应用程序安装到sd卡上-d 允许进行降级安 ...
- 我学习的自定义ASP.NET分页控件
public class MyPagecontroll { public int TotalCount { get; set; }//数据的总条数 public int PageSize { get; ...
- input输入框数字转带千分位的字符串
数字转带千分位的字符串 思路 先获取要转换的数字,对其进行分割 小数部分具体需要保留多少位,具体处理 整数部分用正则做替换 将小数部分和整数部分合计 代码 注意: 本文是基于 jQuery 写的,稍稍 ...
- QList和QVector等容器的区别:(转)
源地址:https://blog.csdn.net/qq_33266987/article/details/53333373 Qlist.QVector 与 list.vector似乎不太类似: li ...
- ES6 数组扩展(总结)
1.扩展运算符 将一个数组转为用逗号分隔的参数序列 console.log(1, ...[2, 3, 4], 5) // 1 2 3 4 5 2.Array.from() 将两类对象转为真正的数组 类 ...
- Python中Class中的object是什么意思?
https://stackoverflow.com/a/2588667/8189120 In short, it sets free magical ponies. In long, Python 2 ...
- Delphi 方法:overload、override、virtual、dynamic、abstract
1.overload 在Pascal语法规则中,同一个UNIT里是不能存在两个同名的函数的,例如: function func(): Boolean; function func(const x: C ...
- MySql查询结果按照指定顺序排序
Mysql这功能做的很好用啊!! 让查询结果按照指定顺序排序 --------------------------------------------------------------------- ...