Alex
Alex

Reputation: 1160

How to get Environment Variable in csproj file?

I want to get the environment variable in csproj, because there I have a condition which exclude appsettings from publish.

enter image description here enter image description here
I want this because, my appsettings didn't depends to Solution Configuration, them depends only from environment variables.

Instead of '$(Configuration)' != Debug' I want something like 'envVariable != Development' etc.

Or is it another method to exclude those files regarding to env variables?

in C# is this method: Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT ").

Upvotes: 43

Views: 25468

Answers (1)

Barak Gall
Barak Gall

Reputation: 1560

So I ran into this same problem today and got it working rather easily. This was one of the first relevant results on google when I searched for this so thought I'd share.

Actually the $() operator is used to resolve any variable within the .csproj but it's also seeded with environment variables already when MSBuild is triggered. So in your case you could do $(envVariable) or $(ASPNETCORE_ENVIRONMENT).

They're brought in like any other .csproj variable.

Upvotes: 57

Related Questions