I created an .Net Core Web Api with an Angular ClientApp with the templates from Visual Studio.
When building the project also builds the contained Angular App with the params set in the .csproj <Target> section e.g.
<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
<Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build -- --prod" />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:ssr -- --prod" Condition=" '$(BuildServerSideRenderer)' == 'true' " />
<!-- Include the newly-built files in the publish output -->
<ItemGroup>
<DistFiles Include="$(SpaRoot)dist\**; $(SpaRoot)dist-server\**" />
<DistFiles Include="$(SpaRoot)node_modules\**" Condition="'$(BuildServerSideRenderer)' == 'true'" />
<ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
<RelativePath>%(DistFiles.Identity)</RelativePath>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</ResolvedFileToPublish>
</ItemGroup>
</Target>
(This section was auto-generated when creating a new project)
Yet this leaves me no choice which environment.ts file from Angular should be used during a build, which makes building for different deployment targets a bit complicated.
Is there any way to set this file dynamically during build? e.g.
- appsettings.json should build with environment.ts
- appsettings.Development.json should build with environment.dev.ts
- appsettings.Production.json should build with environment.prod.ts
- etc.
This would ensure that every dotnet build and its corresponding Angular build would have the correct environment values for the api-url, version, etc...
Or is there another (cleaner?) method to use/override environment variables for the Angular App?
These are the values that should be exchanged during build
export const environment = {
production: false,
dataServiceURI: 'https://localhost:5001/data',
version: 'localhost'
};
As localhost:5001 is no viable option in prod for example
I would just like to provide an end-to-end solution, because it took quite some time that I will save you now :).
My goal was to have different environment configurations for different companies, as the web application will be used by different companies with just small changes (enable/disable modules, change logo, change theme colors, etc.).
launchSettings.json
(within your web project under properties). Add the following code underprofiles
:Where
Staging
is the name if your desired environment name. This will make sure you are able to select this configuration underStart
within Visual Studio.For more info: read this
Add your own
appsettings.Staging.json
file in the root directory of the project for custom app settings. Although this is mostly important for local testing because in your CI/CD you will probably want to configure substitutions. If you do want to use the specificappsettings.Staging.json
in production, then setASPNETCORE_ENVIRONMENT
toStaging
at the server (for azure App Service under Configuration => Application Settings).In your
Startup.cs
, add the followingelse if
block.Notice the
#if DEBUG
, this is required because you don't want to execute this on a deployed environment, however for locally running angular, you HAVE to call this, else you will end up with an error page. As you might also notice, I appenddebug
to the environment name, I will explain later. For now you have to realize thatenv.IsDevelopment()
will be false, and therefor you will have to work around to make sure your angular app will still be build and served when running your app locally.angular.json
, underarchitect => build => configurations
, add 2 new configurations:If you know a bit about
angular.json
you notice that instaging
I make a configuration for a production environment, I will make sure my CI/CD will call this (or when I publish my application via VS2017). For local testing, I hold a different configurationstagingdebug
, so that it still keeps the source maps and I am able to debug my code while running it locally.Add your
environment.staging.ts
file within theenvironments
folder, for environment-specific options. More on that, read thismodify your
package.json
, underscripts
, I added these scripts:Same here: for local development I call the configuration with
debug
appended to it.As of now, you should be able to change to the new configuration within Visual Studio and run according to your new environment configurations!
.csproj
, thanks for that pjominet. Please note that once again, this is only for publishing your web application, not for local debugging.I modified mine to call the correct
script
that I defined in mypackage.json
, because for some reason my CI did not pickup on the extra commands behind it while I was using pjominet's answer.If you are using Azure DevOps, you just have to change the
BuildConfiguration
variable is set toStaging
. If you followed all the steps then this should build successfully.I kind of found a solution by manipulating the *.csproj after all.
HINT: To be clear this problem only applies when you created your project with the
Visual Studio .Net Core Web Api with Angular Template
which creates a combined web api with clientapp projectAfter I found out that I could add custom Build Configurations for
dotnet build
I added the following lines to the<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
block:and in package.json add to scripts:
What it does, is on different build configs set by the
Condition
attribute,npm run build
is called with a corresponding/appropriate environment.Another, perhaps more easy approach, would be to have two build pipelines, one for npm and the angular app and one for the dotnet web api. While doing so, you may want to remove the whole build SPA stuff from the *.csproj otherwise it will build the app twice.
Or just have 2 separate projects in the first place and spare yourself the hassle :)
EDIT:
As pointed out
Command="npm run build --configuration=production"
(e.g. build with mutliple params) is not picked up by the CI/CD. So using predefined scripts from package.json is the correct way to go :)