前言

通常在应用程序开发到正式上线,在这个过程中我们会分为多个阶段,通常会有 开发、测试、以及正式环境等。每个环境的参数配置我们会使用不同的参数,因此呢,在asp.net core中就提供了相关的环境api,方便我们更好的去做这些事情。

环境

asp.net core使用aspnetcore_environment来标识运行时环境。

asp.net core预设环境

  • development:开发环境
  • staging:暂存环境(测试环境)
  • production:正式环境

要取得系统变量aspnetcore_environment,在3.0版本之前可以通过注入ihostingenvironment来获取,3.x通过iwebhostenvironment 请看如下代码片段:

  public class startup
  {
    public void configure(iapplicationbuilder app, iwebhostenvironment env)
    {
      if (env.isdevelopment())
      {
      }

      app.run(async (context) =>
      {
        await context.response.writeasync(
          $"environmentname: {env.environmentname},isdevelopment: {env.isdevelopment()}"
        );
      });
    }
  }

网站启动后iwebhostenvironment会从aspnetcore_environment中获取内容,该变量可以是我们需要的任何值。也就是该变量不一定要一定是预设的值,我们是可以自定义的。

比如我们定义一个名为test环境

   public void configure(iapplicationbuilder app, iwebhostenvironment env)
    {
      env.environmentname = "test";

      if (env.isdevelopment())
      {
        //todo
      }else if (env.isenvironment("text"))
      {
        //todo
      }

      app.run(async (context) =>
      {
        await context.response.writeasync(
          $"environmentname: {env.environmentname},isdevelopment: {env.isdevelopment()}"
        );
      });
 }

注:在 windows 和 macos 上,环境变量和值不区分大小写。 默认情况下,linux 环境变量和值要区分大小写 。

    public static ihostbuilder createhostbuilder(string[] args) =>
      host.createdefaultbuilder(args)
        .configureappconfiguration((hostcontext, config) =>
        {
          var env = hostcontext.hostingenvironment;
          config.setbasepath(path.combine(env.contentrootpath, "configuration"))
            .addjsonfile(path: "settings.json", optional: false, reloadonchange: true)
            .addjsonfile(path: $"settings.{env.environmentname}.json", optional: true, reloadonchange: true);
        })
        .configurewebhostdefaults(webbuilder =>
        {
          webbuilder.usestartup<startup>();
        });

通过上述代码我们,读取我们的配置文件回显读取setting.json并设置为optional: false,表示该配置为必要的配置;再往下继续读取再读取settings.{env.environmentname}.json文件。当加载遇到相同的key那么就会覆盖掉前面的配置项。

  • setbasepath:设置配置的目录位置,如果是放在不同目录,再把路径换掉即可。
  • addjsonfile:
    • path:文件的路径位置。
    • optional:如果是必要的配置文件,可选就要设定为false,当文件不存在就会引发filenotfoundexception。
    • reloadonchange:如果文件被更新,就同步更新iconfiguration实例的值。

环境设置

iis

web.config配置环境变量

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <system.webserver>
  <handlers>
   <add name="aspnetcore" path="*" verb="*" modules="aspnetcoremodule" resourcetype="unspecified" />
  </handlers>
  <aspnetcore processpath="dotnet" arguments=".\demo.dll" stdoutlogenabled="false" stdoutlogfile=".\logs\stdout">
   <environmentvariables>
    <environmentvariable name="aspnetcore_environment" value="test" />
   </environmentvariables>
  </aspnetcore>
 </system.webserver>
</configuration>

visual studio code

launch.json中配置aspnetcore_environment

{
  "version": "0.1.0",
  "configurations": [
    {
      "name": ".net core launch (web)",
      "type": "coreclr",
      "env": {
        "aspnetcore_environment": "development"
      }
    }
  ]
}

visual studio ide

properties\launchsettings.json

 "profiles": {
  "iis express": {
   "commandname": "iisexpress",
   "launchbrowser": true,
   "environmentvariables": {
    "aspnetcore_environment": "test"
   }
  },
  }

到此这篇关于asp.net core 配置和使用环境变量的实现的文章就介绍到这了,更多相关asp.net core 配置和环境变量内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!