How to configure dotnet core web.config from dotnet publish?
Asked Answered
A

2

6

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?

Albuminate answered 23/9, 2020 at 15:52 Comment(0)
B
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?

Baldric answered 24/9, 2020 at 5:36 Comment(4)
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?Albuminate
dotnet publish -c Debug /p:EnvironmentName=Development /p:URLS=http%3A%2F%2Flocalhost%2Fapp /p:LOCAL=trueAlbuminate
@MattW did you tried to use another way to add variable?Baldric
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.Albuminate
C
3

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

Chug answered 30/6, 2022 at 9:26 Comment(1)
Just used this successfully with Umbraco 10 so adding this comment for other future Googlers. Thanks.Tablecloth

© 2022 - 2024 — McMap. All rights reserved.