Reputation: 61596
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.
Upvotes: 8
Views: 25470
Reputation: 45105
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?
Upvotes: 1
Reputation: 517
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>
Upvotes: 2
Reputation: 106
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>
Upvotes: 4
Reputation: 765
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
Step 2 :
now select system.webserver/asp.netcore and in other dropdown select applicationHost.config shown in below image.
Step 3 :
Now select enviromentVariable and enter value.
I hope it may help you.
Upvotes: 10
Reputation: 29966
For publishing the web.config
with specific environment, you could try Transform web.config.
For your requirement, you could try Profile
.
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>
Create your project profile with the specific name like Dev
and Release
, you could create the web.{profile}.config with your existing profile name.
After publishing, it will copy the specific content from web.{profile}.config
to the web.config
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>
Upvotes: -1