zoukankan      html  css  js  c++  java
  • 在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";
    }
    <h2>@ViewData["Title"]</h2>
    <h3>@Model.Message</h3>
    
    <p> ASPNETCORE_ENVIRONMENT = @hostingEnv.EnvironmentName</p>
    
    <environment include="Development">
        <div>&lt;environment include="Development"&gt;</div>
    </environment>
    <environment exclude="Development">
        <div>&lt;environment exclude="Development"&gt;</div>
    </environment>
    <environment include="Staging,Development,Staging_2">
        <div>
            &lt;environment include="Staging,Development,Staging_2"&gt;
        </div>
    </environment>

    提示:在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可以暴露一些不能在产品模式下暴露的异常

    本地机器开发环境变量的设置在PropertieslaunchSettings.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)
  • 相关阅读:
    在ASP.NET MVC2的Html.BeginForm中定义Form的id和name
    MVC中使用Ajax提交数据 Jquery Ajax方法传值到action
    用XML编写EXCEL文件,XML的写法注意事项,可以C#+xslt导出Excel
    DataGrid中删除分页最后一条记录时PageIndex错误的解决方法
    el 表达式函数对数组的处理
    solr 3.5 配置及应用(三)
    centos 6.2 syslogng的配置
    CentOS 6.2 安装tripwire2.4.2.2配置
    日志服务器的配置
    solr 3.5 配置及应用(一)
  • 原文地址:https://www.cnblogs.com/tangchun/p/9024920.html
Copyright © 2011-2022 走看看