Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

How do I publish a .NET Core + Angular single page app for different environments?

Some additional context: I created a solution using the Angular project template with ASP.NET Core in Visual Studio 2019. The app has several different environments: DEV, STG, and Production. There are corresponding "appsettings.json" files for the .NET project ("appsettings.DEV.json", "appsettings.STG.json", "appsettings.Production.json"), as well as "environment.ts" files in the "ClientApp" folder ("environment.DEV.ts", "environment.STG.ts", "environment.Production.ts"). This app will be deployed to IIS on a Windows Server, and I have created ".pubxml" publish profiles targeting a folder for each of these environments.

The project template generates the following in the .csproj file:

<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' " />
</Target>

The .pubxml files I created set the value of ASPNETCORE_ENVIRONMENT like this:

<environmentVariables>
  <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="DEV" />
</environmentVariables>

I have also added build scripts for the Angular client app in the "package.json" file for each environment:

"scripts": {
  "build-dev": "ng build -c DEV",
  "build-stg": "ng build -c STG",
  "build-prod": "ng build -c Production",
}

However, I would like to specify which of those scripts to run, depending on which publish profile was selected. I tried modifying the ".csproj" file with the following, but it does not work. I can't figure out how to get the value of ASPNETCORE_ENVIRONMENT in the .csproj file:

<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-dev" Condition="'$(ASPNETCORE_ENVIRONMENT)' == 'DEV'" />
  <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build-stg" Condition="'$(ASPNETCORE_ENVIRONMENT)' == 'STG'" />
  <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build-production" Condition="'$(ASPNETCORE_ENVIRONMENT)' == 'Production'" />
</Target>

What is the correct way to publish the entire app, building both the .NET app and the Angular app in the "ClientApp" folder for different environments?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...