4

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.

Ankit Mori
  • 705
  • 14
  • 23
AngryHacker
  • 59,598
  • 102
  • 325
  • 594
  • Possible duplicate of [how to set ASPNETCORE\_ENVIRONMENT to be considered for publishing an asp.net core application?](https://stackoverflow.com/questions/41546943/how-to-set-aspnetcore-environment-to-be-considered-for-publishing-an-asp-net-cor) – yaakov Apr 03 '19 at 21:46
  • @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. – AngryHacker Apr 03 '19 at 21:48
  • If 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 (https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/web-host?view=aspnetcore-2.2#override-configuration) 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 (https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.2#windows) – reggaemahn Apr 03 '19 at 21:50
  • @reggaemahn - basically I need to set the environment variable as a part of publish on a server I am deploying to. – AngryHacker Apr 03 '19 at 21:52
  • If 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? – reggaemahn Apr 03 '19 at 22:03
  • @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. – AngryHacker Apr 03 '19 at 22:09
  • I 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. – reggaemahn Apr 03 '19 at 22:35
  • @reggaemahn Never used hostsettings.json - will have to read up on it. Sounds like a promising route. – AngryHacker Apr 03 '19 at 23:33
  • 1
    @AngryHacker the real answer is buried, but it's there: https://stackoverflow.com/a/54432405/1994390 – yaakov Apr 04 '19 at 02:43
  • which version of asp.net-oore your using ? – Ankit Mori Apr 04 '19 at 07:43
  • refer this article https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.2 – Ankit Mori Apr 04 '19 at 10:31

5 Answers5

7

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

enter image description here

Step 2 :

now select system.webserver/asp.netcore and in other dropdown select applicationHost.config shown in below image.

enter image description here

Step 3 :

Now select enviromentVariable and enter value.

enter image description here

I hope it may help you.

Ankit Mori
  • 705
  • 14
  • 23
  • This answer should be (IMHO) the selected answer. Thanks @AnkitMori for this perfect and detailed answer. I was searching for an hour for this. – Danimal111 Aug 04 '22 at 13:50
4

From https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-3.1#set-the-environment

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>
Blobfisher
  • 86
  • 5
  • I 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. – JustJohn Jul 04 '20 at 21:37
1

After a long search, reading and experimenting, i solved by simply adding the enviroment variable ASPNETCORE_ENVIRONMENT in my .pubxml

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <ASPNETCORE_ENVIRONMENT>Test</ASPNETCORE_ENVIRONMENT>
  </PropertyGroup>
  <PropertyGroup>
    <WebPublishMethod>MSDeploy</WebPublishMethod>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    .....
  </PropertyGroup>
</Project>

I use it to select my react app's build environment, on publish, my .csproj looks like:

  <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:$(ASPNETCORE_ENVIRONMENT.ToLower())" />
    ...
  </Target>

Finally my react app's package.json:

"scripts": {
    "start": "env-cmd -f .env.development react-scripts start",
    "build:production": "env-cmd -f .env.production react-scripts build",
    "build:staging": "env-cmd -f .env.staging react-scripts build",
    "build:test": "env-cmd -f .env.test react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "mock:api": "json-server --watch db.json --port 4000"
  },

I intentionally removed "build": "react-scripts build" line, so environment is always supplied.

EDIT:

I also added PropertyGroup for setting EnvironmentName in .csproj file, so on publishing, my web.config will set the ASPNETCORE_ENVIRONMENT as environment variable, as defined in my .pubxml, dynamically:

  <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:$(ASPNETCORE_ENVIRONMENT.ToLower())" />
    <PropertyGroup>
      <EnvironmentName>$(ASPNETCORE_ENVIRONMENT)</EnvironmentName>
    </PropertyGroup>
  </Target>
G J
  • 477
  • 9
  • 23
1

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?

Luke Puplett
  • 42,091
  • 47
  • 181
  • 266
-1

For publishing the web.config with specific environment, you could try Transform web.config.

For your requirement, you could try Profile.

  1. Create multiple web.{profile}.config with different ASPNETCORE_ENVIRONMENT environment variable.

    eg.web.Dev.config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <location>
        <system.webServer>
        <aspNetCore>
            <environmentVariables xdt:Transform="InsertIfMissing">
            <environmentVariable name="ASPNETCORE_ENVIRONMENT"
                                value="Dev"
                                xdt:Locator="Match(name)"
                                xdt:Transform="InsertIfMissing" />
            </environmentVariables>
        </aspNetCore>
        </system.webServer>
    </location>
    </configuration>
    

    eg. web.Release.config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <location>
        <system.webServer>
        <aspNetCore>
            <environmentVariables xdt:Transform="InsertIfMissing">
            <environmentVariable name="ASPNETCORE_ENVIRONMENT"
                        value="Release"
                        xdt:Locator="Match(name)"
                        xdt:Transform="InsertIfMissing" />
            </environmentVariables>
        </aspNetCore>
        </system.webServer>
    </location>
    </configuration>
    
  2. Create your project profile with the specific name like Dev and Release, you could create the web.{profile}.config with your existing profile name.

  3. After publishing, it will copy the specific content from web.{profile}.config to the web.config

  4. Currently, it will exsit web.config, web.Dev.config and web.Release.config in your publihshed folder, if you prefer only web.config, change your Project.csproj like below to exclude the unwantted files.

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
    <PropertyGroup>
        <TargetFramework>netcoreapp2.2</TargetFramework>
        <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
        <Configurations>Debug;Release;Dev</Configurations>
    </PropertyGroup>
    
    <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.App" />
        <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
    </ItemGroup>
    <ItemGroup>
        <Content Update="web.Dev.config" CopyToPublishDirectory="Never" />
        <Content Update="web.Release.config" CopyToPublishDirectory="Never" />
    </ItemGroup>
    </Project>
    
Edward
  • 28,296
  • 11
  • 76
  • 121