ASP.NET核心应用程序中的环境变量

时间:2022-11-29 03:36:38

How can I use Environment variable in Asp.Net Core? I've already added an environment variable to my system and restarted my PC, but when I run an app and get Environment variables I don't see my variable.

如何在Asp.Net Core中使用Environment变量?我已经为我的系统添加了一个环境变量并重新启动了我的PC,但是当我运行一个应用程序并获取环境变量时,我没有看到我的变量。

Environment.GetEnvironmentVariables();

Moreover, when I add environment in code, it works fine:

而且,当我在代码中添加环境时,它工作正常:

Environment.SetEnvironmentVariable("my-credentials-key", my-value);

Do you have any idea how to add Environment variable and use it in ASP.NET core app?

您是否知道如何添加Environment变量并在ASP.NET核心应用程序中使用它?

2 个解决方案

#1


1  

This is how I am using Environment variables in my applications-

这就是我在我的应用程序中使用环境变量的方式 -

In Visual Studio, using launchSettings.json-

在Visual Studio中,使用launchSettings.json-

  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "MY_TEST":"123"
      }
    },
    "SamplePractice": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "MY_TEST":"123"
      }
    }
  }

Since launchSettings.json is only limited to Visual Studio, In case of publish version we use web.config-

由于launchSettings.json仅限于Visual Studio,如果是发布版本,我们使用web.config-

<aspNetCore processPath="dotnet" arguments=".\MyAspNetCoreApplication.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" >
  <environmentVariables>
    <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
    <environmentVariable name="MY_TEST" value="123" />
  </environmentVariables>
</aspNetCore>

And this environment value will read across application using -

此环境值将使用以下内容在应用程序中读取:

Environment.GetEnvironmentVariable("MY_TEST");

#2


0  

Briefly, in launchSettings.json file you can define environment variables for every single host. For example you can have Staging environment for IIS but development, and testing environments for Self hosting.

简而言之,在launchSettings.json文件中,您可以为每个主机定义环境变量。例如,您可以使用IIS的暂存环境,但可以使用自托管的开发和测试环境。

you can find more information in here.

你可以在这里找到更多信息。

#1


1  

This is how I am using Environment variables in my applications-

这就是我在我的应用程序中使用环境变量的方式 -

In Visual Studio, using launchSettings.json-

在Visual Studio中,使用launchSettings.json-

  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "MY_TEST":"123"
      }
    },
    "SamplePractice": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "MY_TEST":"123"
      }
    }
  }

Since launchSettings.json is only limited to Visual Studio, In case of publish version we use web.config-

由于launchSettings.json仅限于Visual Studio,如果是发布版本,我们使用web.config-

<aspNetCore processPath="dotnet" arguments=".\MyAspNetCoreApplication.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" >
  <environmentVariables>
    <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
    <environmentVariable name="MY_TEST" value="123" />
  </environmentVariables>
</aspNetCore>

And this environment value will read across application using -

此环境值将使用以下内容在应用程序中读取:

Environment.GetEnvironmentVariable("MY_TEST");

#2


0  

Briefly, in launchSettings.json file you can define environment variables for every single host. For example you can have Staging environment for IIS but development, and testing environments for Self hosting.

简而言之,在launchSettings.json文件中,您可以为每个主机定义环境变量。例如,您可以使用IIS的暂存环境,但可以使用自托管的开发和测试环境。

you can find more information in here.

你可以在这里找到更多信息。