【问题标题】:How to publish external Angular folder with my asp.net core project?如何使用我的 asp.net 核心项目发布外部 Angular 文件夹?
【发布时间】:2021-05-15 14:34:11
【问题描述】:

我已经创建了一个带有外部角度项目的 Asp net core api,该应用程序在调试模式下运行良好,我想在同一个文件夹中发布这两个应用程序,但是角度项目似乎被复制到了错误的文件夹中。我得到了灵感来自 Asp Angular 模板 csproj。

我的 csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>

<TargetFramework>net5.0</TargetFramework>

<RootNamespace>rct_api</RootNamespace>

<SpaRoot>..\rct-angular\</SpaRoot>

<!-- Set this to true if you enable server-side prerendering -->

<BuildServerSideRenderer>false</BuildServerSideRenderer>

</PropertyGroup>



<ItemGroup>

<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="5.0.2" />

<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />

</ItemGroup>



<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>

<ExcludeFromSingleFile>true</ExcludeFromSingleFile>

</ResolvedFileToPublish>

</ItemGroup>

</Target>



</Project>

结果:

_UnfilteredPriorPublishFileWrites=

C:\Users\Eddy\Source\Repos\rct-solution\rct-api\obj\Release\net5.0\PubTmp\Out\rct-api.exe

C:\Users\Eddy\Source\Repos\rct-solution\rct-api\obj\Release\net5.0\PubTmp\Out\appsettings.Development.json

C:\Users\Eddy\Source\Repos\rct-solution\rct-api\obj\Release\net5.0\PubTmp\Out\appsettings.json

C:\Users\Eddy\Source\Repos\rct-solution\rct-api\obj\Release\net5.0\PubTmp\Out\package-lock.json

C:\Users\Eddy\Source\Repos\rct-solution\rct-api\obj\Release\net5.0\PubTmp\Out\rct-api.dll

C:\Users\Eddy\Source\Repos\rct-solution\rct-api\obj\Release\net5.0\PubTmp\Out\rct-api.deps.json

C:\Users\Eddy\Source\Repos\rct-solution\rct-api\obj\Release\net5.0\PubTmp\Out\rct-api.runtimeconfig.json

C:\Users\Eddy\Source\Repos\rct-solution\rct-api\obj\Release\net5.0\PubTmp\Out\rct-api.pdb

C:\Users\Eddy\Source\Repos\rct-solution\rct-api\obj\Release\net5.0\PubTmp\Out\Microsoft.AspNetCore.SpaServices.Extensions.dll

C:\Users\Eddy\Source\Repos\rct-solution\rct-api\obj\Release\net5.0\PubTmp\Out\Microsoft.OpenApi.dll

C:\Users\Eddy\Source\Repos\rct-solution\rct-api\obj\Release\net5.0\PubTmp\Out\Swashbuckle.AspNetCore.Swagger.dll

C:\Users\Eddy\Source\Repos\rct-solution\rct-api\obj\Release\net5.0\PubTmp\Out\Swashbuckle.AspNetCore.SwaggerGen.dll

C:\Users\Eddy\Source\Repos\rct-solution\rct-api\obj\Release\net5.0\PubTmp\Out\Swashbuckle.AspNetCore.SwaggerUI.dll

C:\Users\Eddy\Source\Repos\rct-solution\rct-api\obj\Release\net5.0\PubTmp\Out..\rct-angular\dist

Execution de la tâche "ReadLinesFromFile" 终止。

Tâche "ConvertToAbsolutePath"

参数:

pub 临时文件夹似乎是错误的,如何正确?

C:\Users\Eddy\Source\Repos\rct-solution\rct-api\obj\Release\net5.0\PubTmp\Out..\rct-angular\dist\3rdpartylicenses.txt强>

CopyToPublishDirectory=PreserveNewest

ExcludeFromSingleFile=true

RelativePath=..\rct-angular\dist\3rdpartylicenses.txt

【问题讨论】:

  • 从上述配置中,您将 SPA 根路径设置为 rct-angular。据我所知,默认情况下,angular应用在ClientApp文件夹like this,如果你想使用这个文件夹,请尝试参考this link更改配置。
  • @ZhiLv 就像我说我把我的 Angular 项目放在外部文件夹中,我不使用 Visual Studio 的 Angular 模板,我的解决方案:imgur.com/c8bk8UU

标签: angular visual-studio asp.net-core


【解决方案1】:

最后我找到了一个解决方案,我修改了 angular Json 以在 asp 项目的 wwwroot 文件夹中构建:

  "build": {
      "builder": "@angular-devkit/build-angular:browser",
      "options": {
        "outputPath": "../rct-api/wwwroot",
        "index": "src/index.html",

我也更新了我的 csproj:

  <ItemGroup>
  
    <Content Remove="$(SpaRoot)**" />
    <None Remove="$(SpaRoot)**" />
    <None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
  </ItemGroup>

  <ItemGroup>
    <None Include="Pages\Error.cshtml" />
    <None Include="Pages\_ViewImports.cshtml" />
    <None Include="wwwroot" Exclude="wwwroot\**" />
  </ItemGroup>

  <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">   
    <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="wwwroot\**" />
      <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
        <RelativePath>%(DistFiles.Identity)</RelativePath>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
        <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
      </ResolvedFileToPublish>
    </ItemGroup>
  </Target>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-19
    • 2018-07-28
    • 2017-05-03
    • 2016-01-14
    • 1970-01-01
    • 2013-11-26
    • 2021-10-04
    • 1970-01-01
    相关资源
    最近更新 更多