.NetCore + Angular + Azure:部署

塞德里克·阿诺尔德

我在服务器文件夹中创建了一个Web Api Asp.Net核心项目,客户端文件夹中创建了一个Angular-Cli项目

在本地:

  • 我用IIS Express启动WebAPI,

    ng serve --proxy-config proxy.config.json*. 
    
  • 在我的Angular App的environment / environment.ts中,为了能够使用environment.apiUrl获取ApiUrl (代理不适用于SignalR使用的websocket),我添加了一个新参数:

    apiUrl: "http://localhost:49402/" 
    
  • 文件结构:

我项目的文件结构

我的项目也可以在git上使用:https : //github.com/ranouf/AspNetCore-SignalR/

在Azure上:

  • 我创建了一个新的WebApp,

  • 我配置了部署选项,每次修改master时,都会部署适合我的WebApi的Web App。

问题:

如何在Azure的Web应用程序中部署我的Angular应用程序?

我的想法:

我添加了一个后构建命令,该命令仅在发布模式下才能在wwwroot文件夹中构建我的角度应用程序此解决方案在本地有效,但在Azure上无效

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<PostBuildEvent>
  cd "$(MSBuildProjectDirectory)/../../client"
  npm install
  npm run build
</PostBuildEvent>
</PropertyGroup>

在startup.cs中,我添加了

app.UseDefaultFiles();
app.UseStaticFiles();

约束:

我必须在本地将我的2个项目分开,不能使用webpack microsoft angular模板。

谢谢你的帮助。塞德里克


编辑

所以我还在努力。

调试和了解Azure Portal上发生的事情真的很难。登录Deployment不会提供构建输出。

在此处输入图片说明

我的新的预构建命令是:

echo "$(ProjectDir)..\..\client"
cd $(ProjectDir)..\..\client
echo "npm install"
npm install
echo "ng build"
ng build --env=prod --prod --output-path=$(ProjectDir)

But still, nothing is copied in the server folder ...

Please, can you share your thinking? How can I deploy my angular App using Deployment Option in Azure portal?


Edit 2

So, I saw no solution online, and no one answer here too, so what I want to do first seems to not be possible.

I try something else, the kudu deployment script. You need to add two file:

  • .deployment with this lines inside:

    [config]
    command = deploy.cmd
    
  • deploy.cmd:

    @if "%SCM_TRACE_LEVEL%" NEQ "4" @echo off
    
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    :: Setup
    :: -----
    setlocal enabledelayedexpansion
    
    IF NOT DEFINED DEPLOYMENT_SOURCE (
      SET DEPLOYMENT_SOURCE=%ARTIFACTS%\repository
    )
    
    IF NOT DEFINED DEPLOYMENT_TARGET (
      SET DEPLOYMENT_TARGET=%ARTIFACTS%\wwwroot
    )
    
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    :: Deployment
    :: ----------
    echo Source: %DEPLOYMENT_SOURCE%...
    echo Deployment: in %DEPLOYMENT_TARGET%...
    
    echo Server off
    touch %DEPLOYMENT_TARGET%\App_Offline.htm
    
    pushd "%DEPLOYMENT_SOURCE%\src\client"
    echo npm install --production
    call :ExecuteCmd npm install --production
    IF !ERRORLEVEL! NEQ 0 goto error
    
    echo Deploy client
    call :ExecuteCmd npm run build-azure
    IF !ERRORLEVEL! NEQ 0 goto error
    popd
    
    echo Deploy API
    call :ExecuteCmd dotnet publish src\server\AspNetCore-SignalR.Api\ -o %DEPLOYMENT_TARGET%
    IF !ERRORLEVEL! NEQ 0 goto error
    
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    :: End
    :: ---
    IF !ERRORLEVEL! NEQ 0 goto error
    
    goto end
    
    :: Execute command routine that will echo out when error
    :ExecuteCmd
    setlocal
    set _CMD_=%*
    call %_CMD_%
    if "%ERRORLEVEL%" NEQ "0" echo Failed exitCode=%ERRORLEVEL%, command=%_CMD_%
    exit /b %ERRORLEVEL%
    
    :error
    endlocal
    echo An error has occurred during web site deployment.
    call :exitSetErrorLevel
    call :exitFromFunction 2>nul
    
    :exitSetErrorLevel
    exit /b 1
    
    :exitFromFunction
    ()
    
    :end
    endlocal
    echo Finished successfully.
    

There is 2 good points:

  • the deployment succeded
  • all the files (DotNet Core publish + Angular publish) are in wwwroot

There is 1 BIG problem:

  • I can access to the API using swagger (www.website.com/swagger) but the angular App is not available (error 404 on www.webiste.com)
  • If I remove the web.config, the Angular App is now available, but not swagger or the API anymore.

How to make understand to the web.config that I want both work Angular App and Swagger?

Cedric Arnould

I found the solution! It was hard, I tried many things even the Kudu Deploy script:

(for those who could be interested to see how to do that).

But finally, I choose the (maybe) easiest solution, the PreBuildEvent command.

In my csproj:

<PropertyGroup Condition="'$(Configuration)' == 'Release'">
  <PreBuildEvent>
    cd "$(MSBuildProjectDirectory)/../../client"
    npm install
  </PreBuildEvent>
  <PostBuildEvent>
    cd "$(MSBuildProjectDirectory)/../../client"
    npm run build-azure
  </PostBuildEvent>
</PropertyGroup>

In Package.json:

"build": "ng build --env=prod --prod --output-path=../server/AspNetCore-SignalR.Api/wwwroot/",
"build-azure": "ng build --env=prod --prod --output-path=D:/home/site/wwwroot/wwwroot/",

The main things to know is when you want to deploy an Angular App, you need to do it in wwwroot folder. In Azure, the .Net app is deployed in D:/home/site/wwwroot/. My mistake was to think that I have to deploy my angular app there too. The good thing to do is to deploy it in D:/home/site/wwwroot/wwwroot.

我希望我的工作也会对人们有所帮助。如果您有任何改进流程和增强安全性的建议,请告诉我们。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在 .netcore 3.0 中部署 angular8 生产版本

将Angular Universal部署到Azure

在Azure上部署Angular 5 build:ssr

将.NetCore Angular 6发布到Azure时出错

带有Angular和.NETCore2 WEBAPI的Azure AD Auth

使用 Angular 前端部署 .NETCore API 时出错 - HTTP 错误 500.0 - ANCM 进程内处理程序加载失败

如何在 Azure 中部署 netcore IHosted Service 控制台应用程序?

将Angular 2应用程序部署到Azure

将 Angular 2 应用程序部署到 Azure

部署Azure Web App(Angular)无法正常工作

将 Angular 项目从 GitHub 部署到 Azure 应用服务

如何捆绑.NetCore API + Angular前端

AspNetZero .NetCore + Angular 项目 - api 版本控制

.NetCore Azure Servicebus获取当前消息数

.netcore和Azure的Newtonsoft版本问题

使用Azure部署组的Angular 8 APP的Azure CI / CD管道

将Angular App部署到Azure Web Apps Angular 9的问题

如何调试失败的NetCore AWS Elastic Beanstalk部署?

从 netcore 部署脚本在子目录中执行 npm 命令

将.NetCore3.1 API部署到LinuxCentos(Nginx)

Visual Studio 2017-部署到Azure Function v2(.NetCore v2.1)错误:所需的进程(“ Web管理服务”)已启动

将Angular 4添加到ASP.NETCore项目

如何将 qrcode 从 .netcore api 传递到 angular

将 PDF 从 Angular 发送到 .NetCore 服务器?

尝试在 azure vm 中部署 angular 应用程序时出错:

如何在 azure 上使用 asp.net mvc 结构部署 angular 5

将 Angular2 应用程序发布和部署到 Azure

将Angular 7应用程序部署到Azure Web App

无法将ASP.NET Core 2.1 Angular应用程序部署到Azure