服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - ASP.NET教程 - ASP.NET Core 配置和使用环境变量的实现

ASP.NET Core 配置和使用环境变量的实现

2021-12-01 15:41HueiFeng ASP.NET教程

这篇文章主要介绍了ASP.NET Core 配置和使用环境变量的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前言

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

环境

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

asp.net core预设环境

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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环境

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  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 环境变量和值要区分大小写 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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配置环境变量

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
{
  "version": "0.1.0",
  "configurations": [
    {
      "name": ".net core launch (web)",
      "type": "coreclr",
      "env": {
        "aspnetcore_environment": "development"
      }
    }
  ]
}

visual studio ide

properties\launchsettings.json

?
1
2
3
4
5
6
7
8
9
"profiles": {
 "iis express": {
  "commandname": "iisexpress",
  "launchbrowser": true,
  "environmentvariables": {
   "aspnetcore_environment": "test"
  }
 },
 }

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

原文链接:https://www.cnblogs.com/yyfh/p/12339961.html

延伸 · 阅读

精彩推荐