Reputation: 5703
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.
Upvotes: 4
Views: 8493
Reputation: 19997
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.
Upvotes: 5
Reputation: 2313
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
Upvotes: 3