4

Here my web.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
      <environmentVariables>
        <environmentVariable name="TEST_WEBCONFIG_VARIABLE" value="some test webconfig variable value" />
      </environmentVariables>
    </aspNetCore>
  </system.webServer>
</configuration>

How can I read TEST_WEBCONFIG_VARIABLE from my web.config in Startup.cs?

I tried Configuration["TEST_WEBCONFIG_VARIABLE"], but this variable doesn't exist in configuration values list.

3
  • 1
    Are you running from visual studio? Commented May 11, 2017 at 11:28
  • 1
    @Sanket yes, does it matter? Commented May 11, 2017 at 16:14
  • 1
    Yuriy - check my answer below. Commented May 12, 2017 at 16:18

2 Answers 2

5

While running from Visual Studio, use launchSettings.json like this-

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

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

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

And this environment value will read across application using -

Environment.GetEnvironmentVariable("TEST_WEBCONFIG_VARIABLE");

NOTE! that it only works in case of publish.

Sign up to request clarification or add additional context in comments.

6 Comments

That's the problem - Environment.GetEnvironmentVariable("TEST_WEBCONFIG_VARIABLE") returns null.
Did you added TEST_WEBCONFIG_VARIABLE in launchSettings.json while running from Visual Studio?
No, do I need it? I thought that TEST_WEBCONFIG_VARIABLE in web.config would be enough.
While running from visual studio, env variable pick up from launchsetting.json. in case of publish, it is pick up from web.config.
Ah, that's explain everything, I'll try to publish it then!
|
3

Pretty sure you just need to call:

For individual setting: Environment.GetEnvironmentVariable("TEST_WEBCONFIG_VARIABLE"‌​);

For list of settings: Environment.GetEnvironmentVariables().GetEnumerator();

Because you are accessing EnvironmentVariables from config not AppSettings

2 Comments

No, it doesn't work, first variant returns null, second - returns all environment variables, but without TEST_WEBCONFIG_VARIABLE.
Hmmmmm. Let me take a quick look into my project

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.