在ASP.NET Core中使用多环境

原文地址:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.1#startup-conventions

ASP.NET Core支持在运行时设置应用程序环境变量

ASP.NET Core在启动时读取环境变量ASPNETCORE_ENVIRONMENT,存储在IHostingEnvironment.EnvironmentName.

ASPNETCORE_ENVIRONMENT 可以设置为任意值,但是在框架内预设的三个值  Development  开发, Staging 预发布  ,Production 产品  ;如果环境变量未设置,默认值是Production产品

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseBrowserLink();
    }

    if (env.IsProduction() || env.IsStaging() || env.IsEnvironment("Staging_2"))
    {
        app.UseExceptionHandler("/Error");
    }

    app.UseStaticFiles();
    app.UseMvcWithDefaultRoute();
}

上面的代码:

  在开发模式下,使用开发异常页,提供更加详细的异常页 和启用浏览器链接

  在产品、预发布、预发布2模式下,在使用ExceptionHandlerMiddleware异常处理中间件

 

使用 Environment Tag Helper 使用IHostingEnvironment.EnvironmentName的值来包含和排除html元素,一般根据开发和产品环境不同,来包含和排除脚本和样式:

@page
@inject Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnv
@model AboutModel
@{
    ViewData["Title"] = "About";
}

@ViewData["Title"]

@Model.Message

ASPNETCORE_ENVIRONMENT = @hostingEnv.EnvironmentName

"Development">
<environment include="Development">
"Development">
<environment exclude="Development">
"Staging,Development,Staging_2">
<environment include="Staging,Development,Staging_2"&gt;

提示:在Windows和macOS下变量和值是不区分大小写的。默认情况下,Linux 环境下变量和值是区分大小写的。

所以默认情况下使用HostingEnvironmentExtensions 下的

IsDevelopment(IHostingEnvironment)

Checks if the current hosting environment name is Development.

IsEnvironment(IHostingEnvironment, String)

Compares the current hosting environment name against the specified value.

IsProduction(IHostingEnvironment)

Checks if the current hosting environment name is Production.

IsStaging(IHostingEnvironment)

Checks if the current hosting environment name is Staging.

方法判断环境变量的值,其内部使用的是StringComparison.OrdinalIgnoreCase 不区分大小写来判断相等的

public static bool IsEnvironment(this IHostingEnvironment hostingEnvironment, string environmentName)
        {
            if (hostingEnvironment == null)
            {
                throw new ArgumentNullException("hostingEnvironment");
            }
            return string.Equals(hostingEnvironment.EnvironmentName, environmentName, (StringComparison)(?)5);
        }

 

Development

在开发模式中使用DeveloperExceptionPage可以暴露一些不能在产品模式下暴露的异常

本地机器开发环境变量的设置在Properties\launchSettings.json文件里面,在launchSettings.json设置的值会覆盖设置在系统环境的值

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:54339/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WebApp1": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging"
      },
      "applicationUrl": "http://localhost:54340/"
    },
    "Kestrel Staging": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_My_Environment": "1",
        "ASPNETCORE_DETAILEDERRORS": "1",
        "ASPNETCORE_ENVIRONMENT": "Staging"
      },
      "applicationUrl": "http://localhost:51997/"
    }
  }
}

applicationUrl 值可以设置多个链接,使用分号来分隔

"WebApplication1": {
   "commandName": "Project",
   "launchBrowser": true,
   "applicationUrl": "https://localhost:5001;http://localhost:5000",
   "environmentVariables": {
     "ASPNETCORE_ENVIRONMENT": "Development"
   }
}

当运用程序通过 dotnet run启动时,将使用commandName的第一个值来启动指定的web服务器。其值可以是下面中的一个:

  • IIS Express
  • IIS
  • Project (which launches Kestrel)

 

转载于:https://www.cnblogs.com/tangchun/p/9024920.html

你可能感兴趣的:(在ASP.NET Core中使用多环境)