4

I have this in my dotnet core 3.1 webapp tasks.json:

        "env": {
            "ASPNETCORE_ENVIRONMENT": "Development",
            "ASPNETCORE_URLS": "http://localhost:5000",
            "LOCAL": "true"
        },

When I run dotnet publish this is what the Debug web.config contains:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\App.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="OutOfProcess" />
    </system.webServer>
  </location>
</configuration>

Because I want to host under IIS, this is what I want it to contain, so for now I need to manually edit it:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\App.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="OutOfProcess">
        <environmentVariables>
          <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
          <environmentVariable name="ASPNETCORE_URLS" value="http://localhost/app" />
          <environmentVariable name="LOCAL" value="true" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>

How can I configure either my solution or the .vscode .json files to make this change when I run dotnet publish?

Matt W
  • 11,753
  • 25
  • 118
  • 215

2 Answers2

6

1)You could use the below command to add the environment variable at the time of publishing:

dotnet publish -c Debug -r /p:EnvironmentName=Development

2)Add environmnet variable in publish profile

<PropertyGroup>
  <EnvironmentName>Development</EnvironmentName>
</PropertyGroup>

3)Modify the project file (.CsProj) file

Referance link:

Publish to IIS, setting Environment Variable

how to set ASPNETCORE_ENVIRONMENT to be considered for publishing an asp.net core application?

Jalpa Panchal
  • 8,251
  • 1
  • 11
  • 26
  • Thanks - I can see the EnvironmentName parameter sets the ASPNETCORE_ENVIRONMENT value in the web.config, but the ASPNETCORE_URLS and LOCAL values do not get touched. Is there a trick to setting each value or do I need to use a separate method for each 'type' of var? – Matt W Sep 24 '20 at 09:33
  • dotnet publish -c Debug /p:EnvironmentName=Development /p:URLS=http%3A%2F%2Flocalhost%2Fapp /p:LOCAL=true – Matt W Sep 24 '20 at 10:09
  • @MattW did you tried to use another way to add variable? – Jalpa Panchal Sep 29 '20 at 09:04
  • I've tried everything here, unfortunately most of the suggestions in the links do not relate to my issue because I'm trying to get the vars produced in the published web.config, not add the during the dotnet core runtime - my solution is IIS hosted. – Matt W Sep 29 '20 at 12:39
2

If you want to do more than just setting the EnvironmentName:

On a project using "Microsoft.NET.Sdk.Web" target, config transforms are applied.

Add a web.{CONFIGURATION}.config in the project folder:

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

Then after running a dotnet publish -c {CONFIGURATION}, the publish output contains the following auto generated web.config (some parts removed for brevity):

...
<aspNetCore ...>
  <environmentVariables>
    <environmentVariable name="Configuration_Specific" value="Configuration_Specific_Value"/>
  </environmentVariables>
</aspNetCore>
...

See https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/transform-webconfig?view=aspnetcore-6.0#build-configuration

w5l
  • 5,341
  • 1
  • 25
  • 43