Talking appsettings.json in Asp.Net Core
在ASP.NET Core中,默认提供了三个运行时环境变量,通过查看Hosting源代码我们可以看到,分别是Development、Staging、Production
public static class EnvironmentName
{
public static readonly string Development = "Development";
public static readonly string Staging = "Staging";
public static readonly string Production = "Production";
}
当启动一个ASP.NET Core应用程序时,会确定当前应该运行哪个环境中。默认情况下,如果没有指定环境变量,会自动默认为Production
public class HostingEnvironment : IHostingEnvironment, Extensions.Hosting.IHostingEnvironment
{
public string EnvironmentName { get; set; } = Hosting.EnvironmentName.Production;
public string ApplicationName { get; set; }
public string WebRootPath { get; set; }
public IFileProvider WebRootFileProvider { get; set; }
public string ContentRootPath { get; set; }
public IFileProvider ContentRootFileProvider { get; set; }
}

在新创建的ASP.NET Core Web Application中我们会看到了两个配置文件分别是appsettings.json和appsettings.Development.json。事实上我们还可以添加两个文件分别是appsettings.Staging.json和appsettings.Production.json,分别是预生产环境和生产环境。根据环境变量的名称会加载具体的配置文件。
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
可以用面相对象的方式理解这几个文件,appsettings.json作为父类,其他几个文件作为子类,当两个配置文件中定义了同一个节点,会以子类的配置为准,相当于orverwrite。如果对应的配置文件中没有找到节点,会从父类中去查找。

如果我们的应用程序运行在Docker容器中,Docker也允许在Dockerfile中使用ENV指定环境变量
FROM microsoft/aspnetcore:2.0.5 WORKDIR /app EXPOSE 80 COPY . . ENV ASPNETCORE_ENVIRONMENT Production ENTRYPOINT ["dotnet", "WebApplication1.dll"]
还有一种方式是在运行容器的时候使用-e参数
docker run -e ASPNETCORE_ENVIRONMENT=Development -p 5000:5000 <image>:<tag>
Talking appsettings.json in Asp.Net Core的更多相关文章
- 用"hosting.json"配置ASP.NET Core站点的Hosting环境
通常我们在 Prgram.cs 中使用硬编码的方式配置 ASP.NET Core 站点的 Hosting 环境,最常用的就是 .UseUrls() . public class Program { p ...
- How to use Bundle&Minifier and bundleconfig.json in ASP.NET Core
引言 我们在ASP.NET MVC 中经常会用到 bundleConfig.cs 文件来进行我们 css 和 js 的绑定, 那么在ASP.NET Core 中我们应该如何使用呢? 步骤一 在 Vis ...
- Adding appsettings.json to a .NET Core console app
This is something that strangely doesn’t seem to be that well documented and took me a while to figu ...
- .net core 读取appsettings.json乱码
.net core 读取配置文件乱码:vs2019读取appsettings.json乱码问题; .net core 读取appsettings.json乱码问题;用notepad++或者其他编辑器打 ...
- Use Dapper ORM With ASP.NET Core
Dapper.NET is not just another ORM tool, it's considered as the king of ORM. Because it's fast, easy ...
- ASP.NET Core Web App应用第三方Bootstrap模板
引言 作为后端开发来说,前端表示玩不转,我们一般会选择套用一些开源的Bootstrap 模板主题来进行前端设计.那如何套用呢?今天就简单创建一个ASP.NET Core Web MVC 模板项目为例, ...
- ASP.NET Core 请求/查询/响应参数格式转换(下划线命名)
业务场景: 在 ASP.NET Core 项目中,所有的代码都是骆驼命名,比如userName, UserName,但对于 WebApi 项目来说,因为业务需要,一些请求.查询和响应参数的格式需要转换 ...
- Angular 学习笔记 ( timezone + moment + material date-picker + date pipe + asp.net core )
参考 : https://stackoverflow.com/questions/29979609/time-conversion-with-timezoneinfo-for-past-years h ...
- ASP.NET Core 3.0 迁移避坑指南
一.前言 .NET Core 3.0将会在 .NET Conf 大会上正式发布,截止今日发布了9个预览版,改动也是不少,由于没有持续关注,今天将前面开源的动态WebApi项目迁移到.NET Core ...
随机推荐
- 在应用中更新App版本号
在应用中, 为了提高用户体验, 会提供更新版本号的功能. 那么怎样实现呢? 我写了一个简单的Demo, 说明一下, 须要注意几个细节. 使用了Retrofit和Rx处理网络请求. Github下载地址 ...
- 人工智能背景下的Office 365现状和发展趋势
作者:陈希章 发表于 2017年7月31日 引子 谈论人工智能是让人兴奋的,因为它具有让人兴奋的两大特征 -- 每个人都似乎知道一点并且以知道一点为荣,但又好像没多少人能真正讲的明白.毫无疑问,我也仅 ...
- NIO相关基础篇三
转载请注明原创出处,谢谢! 说在前面 上篇NIO相关基础篇二,主要介绍了文件锁.以及比较关键的Selector,本篇继续NIO相关话题内容,主要谈谈一些Linux 网络 I/O模型.零拷贝等一些内容, ...
- 单点登录,session,jsonp(待更新)
单点登录理解: 单点登录系统设计: ajax跨域:
- 小白的Python之路 day4 装饰器前奏
装饰器前奏: 一.定义: 1.装饰器本质是函数,语法都是用def去定义的 (函数的目的:他需要完成特定的功能) 2.装饰器的功能:就是装饰其他函数(就是为其他函数添加附加功能) 二.原则: 1. 不能 ...
- sqlserver 存储过程 游标实例
if exists(select * from sysobjects where id = object_id(N'dbo.test_cursor') and type = 'P') drop PRO ...
- iOS 字体设置,字体类型展示
字体设置: [UIFont fontWithName:@"Helvetica" size:17.0]]; 字体名字,如图 UIFont fontWithName 后不知道字体的名字 ...
- Python3.5:爬取网站上电影数据
首先我们导入几个pyhton3的库: from urllib import requestimport urllibfrom html.parser import HTMLParser 在Python ...
- C语言应用程序的内存图
1.综述 c语言应用程序加载到内存,这时它所占据的内存分为四个区,分别为栈Stack,堆Heap,静态存储区Static Area,代码存储区Code Area,这四个区分别放置应用程序的不同部分,从 ...
- Missing number in array
Given an array of size n-1 and given that there are numbers from 1 to n with one missing, the missin ...