在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的更多相关文章

  1. 用"hosting.json"配置ASP.NET Core站点的Hosting环境

    通常我们在 Prgram.cs 中使用硬编码的方式配置 ASP.NET Core 站点的 Hosting 环境,最常用的就是 .UseUrls() . public class Program { p ...

  2. How to use Bundle&Minifier and bundleconfig.json in ASP.NET Core

    引言 我们在ASP.NET MVC 中经常会用到 bundleConfig.cs 文件来进行我们 css 和 js 的绑定, 那么在ASP.NET Core 中我们应该如何使用呢? 步骤一 在 Vis ...

  3. 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 ...

  4. .net core 读取appsettings.json乱码

    .net core 读取配置文件乱码:vs2019读取appsettings.json乱码问题; .net core 读取appsettings.json乱码问题;用notepad++或者其他编辑器打 ...

  5. 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 ...

  6. ASP.NET Core Web App应用第三方Bootstrap模板

    引言 作为后端开发来说,前端表示玩不转,我们一般会选择套用一些开源的Bootstrap 模板主题来进行前端设计.那如何套用呢?今天就简单创建一个ASP.NET Core Web MVC 模板项目为例, ...

  7. ASP.NET Core 请求/查询/响应参数格式转换(下划线命名)

    业务场景: 在 ASP.NET Core 项目中,所有的代码都是骆驼命名,比如userName, UserName,但对于 WebApi 项目来说,因为业务需要,一些请求.查询和响应参数的格式需要转换 ...

  8. Angular 学习笔记 ( timezone + moment + material date-picker + date pipe + asp.net core )

    参考 : https://stackoverflow.com/questions/29979609/time-conversion-with-timezoneinfo-for-past-years h ...

  9. ASP.NET Core 3.0 迁移避坑指南

    一.前言 .NET Core 3.0将会在 .NET Conf 大会上正式发布,截止今日发布了9个预览版,改动也是不少,由于没有持续关注,今天将前面开源的动态WebApi项目迁移到.NET Core ...

随机推荐

  1. mysql数据表最高速迁移,mysql的存储引擎为:myisam

    本文链接:http://blog.csdn.net/u010670689/article/details/41346689 需求: 开发产品过程中,有个项目分支,数据库须要带数据拷贝,可是表的数据非常 ...

  2. 以pfile或者spfile启动时show parameter pfile的不同结果

    普通启动: SQL> show parameter pfile NAME TYPE VALUE ------------------------------------ ----------- ...

  3. 登录界面 Android简单http get请求(含server端)五 iOS端(特别篇)

    </pre><pre name="code" class="objc">NSDictionary *dict=@{@"user ...

  4. SSM学习(一)搭建基础框架

    不知不自觉,工作也两年多了,由于公司一直用的是ssh框架,所以所学也一直是ssh.直到有一天,服务器被攻击,tomcat目录下总有莫名其妙的一些文件,这些文件通过远程ftp下载了一些病毒和木马,服务器 ...

  5. mysql 常用sql语句 一

    创建数据库 create database if not exists wsp_test default charset utf8 collate utf8_general_ci 使用数据库 use ...

  6. urllib2的基本使用

    urlopen 1 import urllib2 2 3 # 向指定的url发送请求,并返回服务器响应的类文件对象 4 response = urllib2.urlopen("http:// ...

  7. 利用reverse索引优化like语句的方法详解

    在有一些情况下,开发同学经常使用like去实现一些业务需求,当使用like时,我们都知道使用like 前%(like '%111')这种情况是无法使用索引的,那么如何优化此类的SQL呢,下面是一个案例 ...

  8. Eclipse Maven构建WebApp项目资源目录显示不全的原因与解决方式

    一.问题展示 1.Eclipse在使用Maven构建WebApp项目的时候,首先Maven的安装和配置都没有问题的,但是构建项目之后,Maven项目要求的几个必须要有的资源目录显示不了: 问题如下图: ...

  9. 为 Azure IoT Edge 设备部署 Azure Stream Analytics 服务

    在前面的两篇文章<Azure IoT Edge on Windows 10 IoT Core>和<Azure IoT Edge on Raspberry Pi 3 with Rasp ...

  10. Eclipse上Maven环境配置使用 (全)

    Eclipse上Maven环境配置使用 (全) 1. 安装配置Maven: 1.1 从Apache网站 http://maven.apache.org/ 下载并且解压缩安装Apache Maven. ...