How to configure dotnet core web.config from dotnet publish?

Viewed 2723

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?

2 Answers

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://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/transform-webconfig?view=aspnetcore-6.0#build-configuration

Related