Talking appsettings.json in Asp.Net Core
2018-01-15 22:29
543 查看
在ASP.NET Core中,默认提供了三个运行时环境变量,通过查看Hosting源代码我们可以看到,分别是Development、Staging、Production
当启动一个ASP.NET Core应用程序时,会确定当前应该运行哪个环境中。默认情况下,如果没有指定环境变量,会自动默认为Production
在新创建的ASP.NET Core Web Application中我们会看到了两个配置文件分别是appsettings.json和appsettings.Development.json。事实上我们还可以添加两个文件分别是appsettings.Staging.json和appsettings.Production.json,分别是预生产环境和生产环境。根据环境变量的名称会加载具体的配置文件。
可以用面相对象的方式理解这几个文件,appsettings.json作为父类,其他几个文件作为子类,当两个配置文件中定义了同一个节点,会以子类的配置为准,相当于orverwrite。如果对应的配置文件中没有找到节点,会从父类中去查找。
如果我们的应用程序运行在Docker容器中,Docker也允许在Dockerfile中使用ENV指定环境变量
还有一种方式是在运行容器的时候使用-e参数
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>
相关文章推荐
- [转]Setting the NLog database connection string in the ASP.NET Core appsettings.json
- ASP .NET CORE 根据环境变量支持多个 appsettings.json
- ASP .NET CORE 根据环境变量支持多个 appsettings.json
- 阿里云容器服务与ASP.NET Core部署:用 docker secrets 保存 appsettings.Production.json
- How to use Bundle&Minifier and bundleconfig.json in ASP.NET Core
- Create an ASP.NET Core web app in Visual Studio Code
- Adding appsettings.json to a .NET Core console app
- Asp.Net Core 中无法使用 ConfigurationManager.AppSettings
- Create an ASP.NET web app in Azure App Service
- ASP.NET Core读取appsettings.json的配置
- 【无私分享: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有效期问题_时区问题
- Setup Automapper in ASP.NET Core
- 【ASP.NET Core快速入门】(五)命令行配置、Json文件配置、Bind读取配置到C#实例、在Core Mvc中使用Options
- Asp.Net Core中使用Newtonsoft.Json进行序列化处理解决返回值首字母小写
- Dependency injection configurations into views in asp.net core
- Asp.NetCore1.1版本去掉project.json后如何打包生成跨平台包
- Asp.Net Core中Json序列化处理整理
- Azure 部署 Asp.NET Core Web App
- Windows + IIS 环境部署Asp.Net Core App