How to set ASPNETCORE_ENVIRONMENT during publishing?
Solution 1
You can Just Update your webconfig file with below text in section.
<configuration>
<system.webServer>
<aspNetCore .....>
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</configuration>
Or you can follow below step after hosting your application in IIS.
Step 1 : Click on configuration editor as shown in below image
Step 2 :
now select system.webserver/asp.netcore and in other dropdown select applicationHost.config shown in below image.
Step 3 :
Now select enviromentVariable and enter value.
I hope it may help you.
Solution 2
For Windows IIS deployments: Include the property in the publish profile (.pubxml) or project file. This approach sets the environment in web.config when the project is published.
<PropertyGroup>
<EnvironmentName>Development</EnvironmentName>
</PropertyGroup>
Solution 3
I accidentally found a simple answer to the OP's question.
If an environment variable named environmentName
is set when you called dotnet publish
it will incorporate it into your web.config
.
Azure App Service - What modified my web.config?
Related videos on Youtube
AngryHacker
Updated on June 04, 2022Comments
-
AngryHacker almost 2 years
I have several WebDeploy publish profiles that deploy my .NET Core web project to various places (Dev, QA, Stage on IIS). For the application to know where it's running, I need to set the ASPNETCORE_ENVIRONMENT environment variable.
Is it possible to set the ASPNETCORE_ENVIRONMENT environment variable as a part of publishing the application?
P.S. This question doesn't solve anything for me because it shows how to achieve it manually. I would like it to be automatic as a part of deploy a publish profile.
-
yaakov about 5 years
-
AngryHacker about 5 years@yaakov It shows how to do it manually. I already know that part. I want it done automatically based on the publish profile I use.
-
reggaemahn about 5 yearsIf you are trying to change the runtime behaviour and load a different configuration you can use a hostsettings.json file and set the
environment
property (docs.microsoft.com/en-us/aspnet/core/fundamentals/host/…) file and modify that in your release pipeline. But if you want to do this outside of that, the only way that I know of is to specify it before running the application (docs.microsoft.com/en-us/aspnet/core/fundamentals/…) -
AngryHacker about 5 years@reggaemahn - basically I need to set the environment variable as a part of publish on a server I am deploying to.
-
reggaemahn about 5 yearsIf it's a simple website and you know that's the only server you will be deploying to, you can just run the powershell command once (or even set the environment variable manually once) and then forget about it. Are you using a CI/CD pipeline?
-
AngryHacker about 5 years@reggaemahn I deploy to 4 different servers from the IDE (for now). Each one of them has an appsettings.{Environment}.json. Each one of them also has a separate Publish Profile. So when I choose the Publish.QA profile, I'd like that server's ASPNETCORE_ENVIRONMENT environment variable to be set to QA. I get what you say about doing it once and forgetting it, but sometimes these servers are torn down and new one is generated. Sounds like that's impossible with Visual Studio's publish mechanism. Probably will have to do a manual publish script.
-
reggaemahn about 5 yearsI haven't done a VS publish profile in a while but one approach could be to have four different hostsettings files (hostsettings.{Environment}.json). Then you can load them at app startup
new ConfigurationBuilder().AddJsonFile("hostsettings.json", optional: true)
. Then in your publish profile, you can exclude/include the hostsettings file that applies to that profile/environment. -
AngryHacker about 5 years@reggaemahn Never used hostsettings.json - will have to read up on it. Sounds like a promising route.
-
yaakov about 5 years@AngryHacker the real answer is buried, but it's there: stackoverflow.com/a/54432405/1994390
-
Ankit Mori about 5 yearswhich version of asp.net-oore your using ?
-
Ankit Mori about 5 yearsrefer this article docs.microsoft.com/en-us/aspnet/core/fundamentals/…
-
-
JustJohn almost 4 yearsI did this, hoping to have publsihed app change to Production and the app failed to load after I did this. The error just said "An error occurred while starting the application". Not sure why it would not work.