ASP.NET Core 2.2 基础知识(五) 环境
一.环境变量
系统启动时,会读取环境变量 ASPNETCORE_ENVIRONMENT ,并将该变量的值存储在 IHostingEnvironment.EnvironmentName 字段中.如:
新建一个 WebAPI 项目,修改 Configure 方法:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
......
{
app.Run(async context =>
{
context.Response.ContentType = "text/plain;charset=utf-8";//没有这句话,中文会出现乱码.
await context.Response.WriteAsync($"当前环境 : {env.EnvironmentName}");
});
}
......
}

ASPNETCORE_ENVIRONMENT 可以设置为任意值,但是 ASP.NET Core 框架只支持 3 个值:
Development
Staging
Production
如果没有设置该变量的值,则默认 Production
那么问题来了,在哪里设置呢?
方法一 : launchSettings.json
打开上例创建的 WebAPI 项目的 launchSettings.json 文件:
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:53476",
"sslPort":
}
},
"profiles": {
//部署到IIS时,会读取该节点的设置.
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
//"ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Production" //我们修改为 "Production" 看效果
}
},
//在控制台启动时,会读取该节点的设置
"EnvironmentDemo1": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Staging" //我们修改为 "Staging" 看效果
}
}
}
}
IIS下运行效果:

控制台运行效果:

二.基于环境的 Startup 类
当 ASP.NET Core 应用启动时,会在 Program 类中通过 Startup 类启动应用.
ASP.NET Core 提供了基于不同环境选择不同的 Startup 类的方法.但是类名有个约定,只能这样命名:
Startup{EnvironmentName}
EnvironmentName 为当前环境.
示例:
/// <summary>
/// 开发环境启动类
/// </summary>
public class StartupDevelopment
{
public void ConfigureServices(IServiceCollection services){} public void Configure(IApplicationBuilder app, IHostingEnvironment env){}
} /// <summary>
/// 模拟环境启动类
/// </summary>
public class StartupStaging
{
public void ConfigureServices(IServiceCollection services){} public void Configure(IApplicationBuilder app, IHostingEnvironment env){}
} /// <summary>
/// 生产环境启动类
/// </summary>
public class StartupProduction
{
public void ConfigureServices(IServiceCollection services){} public void Configure(IApplicationBuilder app, IHostingEnvironment env){}
}
要让系统根据当前环境选择上述3个启动类中的一个,还需要修改调用方法.
默认的调用方法是这样的:
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
} public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args).UseStartup<Startup>();
}
需要修改成:
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
var assemblyName = typeof(Startup).GetTypeInfo().Assembly.FullName;
return WebHost.CreateDefaultBuilder(args).UseStartup(assemblyName);
}
二.基于环境的 ConfigureServices 方法和 Configure 方法
上代码一目了然:
public void ConfigureStaging(IApplicationBuilder app, IHostingEnvironment env)
{ } public void ConfigureStagingServices(IServiceCollection services)
{ }
ASP.NET Core 2.2 基础知识(五) 环境的更多相关文章
- ASP.NET Core 2.2 基础知识(十八) 托管和部署 概述
为了方便演示,以 .NET Core 控制台应用程序讲解. 我们新建一个控制台应用程序,安装 "Newtonsoft.Json" Nuget 包,然后右键点击该项目,选择" ...
- ASP.NET Core 2.2 基础知识(十二) 发送 HTTP 请求
可以注册 IHttpClientFactory 并将其用于配置和创建应用中的 HttpClient 实例. 这能带来以下好处: 提供一个中心位置,用于命名和配置逻辑 HttpClient 实例. 例如 ...
- ASP.NET Core 2.2 基础知识(六) 配置(内含MySql+EF)
先上一段代码,了解一下 .NET Core 配置数据的结构. 新建一个 控制台项目,添加一个文件 json.json ,文件内容如下: { "country": "cn& ...
- ASP.NET Core 2.2 基础知识(十六) SignalR 概述
我一直觉得学习的最好方法就是先让程序能够正常运行,才去学习他的原理,剖析他的细节. 就好像这个图: 所以,我们先跟着官方文档,创建一个 SignalR 应用: https://docs.microso ...
- ASP.NET Core 2.2 基础知识(十四) WebAPI Action返回类型(未完待续)
要啥自行车,直接看手表 //返回基元类型 public string Get() { return "hello world"; } //返回复杂类型 public Person ...
- ASP.NET Core 2.2 基础知识(十三) WebAPI 概述
我们先创建一个 WebAPI 项目,看看官方给的模板到底有哪些东西 官方给出的模板: [Route("api/[controller]")] [ApiController] pub ...
- ASP.NET Core 2.2 基础知识(十一) ASP.NET Core 模块
ASP.NET Core 应用与进程内的 HTTP 服务器实现一起运行.该服务器实现侦听 HTTP 请求,并在一系列请求功能被写到 HttpContext 时,将这些请求展现到应用中. ASP.NET ...
- ASP.NET Core 2.2 基础知识(十) Web服务器 - Kestrel
ASP.NET Core 应用与进程内的 HTTP 服务器实现一起运行.该服务器实现侦听 HTTP 请求,并在一系列请求功能被写到 HttpContext 时,将这些请求展现到应用中. ASP.NET ...
- ASP.NET Core 2.2 基础知识(九) 使用托管服务实现后台任务
在 ASP.NET Core 中,后台任务作为托管服务实现.托管服务是一个类,而且必须实现 IHostedService 接口,该接口定义了两个方法: StartAsync(CancellationT ...
随机推荐
- [洛谷P2044][NOI2012]随机数生成器
题目大意:给你$m,a,c,X_0,n,g$,求$X_{n+1}=(a\cdot X_n+c) \bmod{m}$,最后输出对$g$取模 题解:矩阵快速幂+龟速乘,这里用了$long\;double$ ...
- VS的ncb、pdb文件分析
原文链接地址:http://blog.csdn.net/changbaolong/article/details/7472685 NCB是"No Compile ...
- [Leetcode] remove element 删除元素
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D
D. Bear and Two Paths time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- POJ2594:Treasure Exploration(Floyd + 最小路径覆盖)
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 9794 Accepted: 3 ...
- codeforces 792CDivide by Three(两种方法:模拟、动态规划
传送门:https://codeforces.com/problemset/problem/792/C 题意:给你一个字符串,要求让你删除最少个数的元素,使得最终答案是没有前导0并且是3的倍数. 题解 ...
- nodejs与sqlite
//打开数据库var db = new sqlite3.Database('xx.db'); // 关闭数据库db.close(); db.run('xx'); // 数据库对象的run函数可以执行 ...
- [NOIp普及组2011]瑞士轮
洛谷题目链接:瑞士轮 题目背景 在双人对决的竞技性比赛,如乒乓球.羽毛球.国际象棋中,最常见的赛制是淘汰赛和循环赛.前者的特点是比赛场数少,每场都紧张刺激,但偶然性较高.后者的特点是较为公平,偶然性较 ...
- Spring发展史
https://www.cnblogs.com/RunForLove/p/4641672.html
- Maven的默认中央仓库以及修改默认仓库&配置第三方jar包从私服下载
当构建一个Maven项目时,首先检查pom.xml文件以确定依赖包的下载位置,执行顺序如下: 1.从本地资源库中查找并获得依赖包,如果没有,执行第2步. 2.从Maven默认中央仓库中查找并获得依赖包 ...