Overriding Environment Variables in ASP.NET Core


There maybe instances where you need to set or override an environment variable for an ASP.Net core site outside of the server administration area (IIS, Azure console, etc.). This can be accomplished by making an entry to the "aspNetCore" section of the web.config.

In the below example I am going to override the environment variable that specifies the environment.

XML for web.config

  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath=".\Apps.MySite.exe" arguments="" stdoutLogEnabled="true" stdoutLogFile=".\logs\mysite-stdout" forwardWindowsAuthToken="false">	<environmentVariables>		<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="development" />	</environmentVariables>	</aspNetCore>
  </system.webServer>