Like most things here this is a simple write up so I remember that something exists when I want it again in the future.
For various reasons I’ve been trying to publish a dot net web application locally to run under IIS, but not wanting to rely on having to set the ASPNETCORE_ENVIRONMENT as an environment variable due to other applications having slightly different appsettings and me not truly being the owner of the deployment environment.
Luckily there is a way to achieve this. When a web application runs under IIS it still uses a web.config, and that web.config can contain a section such as this:
<aspNetCore processPath="dotnet" arguments=".\MyApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="test" />
</environmentVariables>
</aspNetCore>
This can be cumbersome to manually keep updated (and if you were doing that you could just manually update the appsettings anyway). So along comes the publish profiles.
I only want a quick and dirty publish to local file system for these sites so I can copy and paste (yes, horrible I know):
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>FileSystem</WebPublishMethod>
<ExcludeApp_Data>False</ExcludeApp_Data>
<SkipExtraFilesOnServer>True</SkipExtraFilesOnServer>
<MSDeployPublishMethod>InProc</MSDeployPublishMethod>
<SelfContained>false</SelfContained>
<EnvironmentName>test</EnvironmentName>
<_IsPortable>false</_IsPortable>
<_SavePWD>False</_SavePWD>
</PropertyGroup>
</Project>
The key here being the EnvironmentName
which will control the environment variables specific in the web.config.
Update: A slightly alternative way to achieve this is to use the build configuration instead, and set the EnvironmentName in the .csproj file like so:
<PropertyGroup Condition="'$(Configuration)'=='Test'">
<EnvironmentName>test</EnvironmentName>
</PropertyGroup>