【发布时间】:2022-11-03 08:39:14
【问题描述】:
我有一个由像in the tutorial 这样的 ASP.NET 应用程序管理的 Angular 应用程序。在开发环境中,我可以在 ASP.NET 的项目目录中执行dotnet run。然后构建项目和输出
info: Microsoft.Hosting.Lifetime[14]
Now listening on: https://localhost:5273
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: production
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\Dev\MyApp\src\MyApp.Client\
info: Microsoft.AspNetCore.SpaProxy.SpaProxyLaunchManager[0]
No SPA development server running at https://localhost:44459 found.
info: Microsoft.AspNetCore.SpaProxy.SpaProxyLaunchManager[0]
SPA development server running at 'https://localhost:44459'
info: Microsoft.AspNetCore.SpaProxy.SpaProxyMiddleware[0]
SPA proxy is ready. Redirecting to https://localhost:44459
运行开发脚本,端口 5273 或端口 44459 将重定向到实际的 Angular 应用程序。
但是当我运行dotnet publish --configuration test 然后运行dotnet bin/test/net6.0/MyApp.Client.dll 时,ASP.NET 应用程序得到了服务,但是导航到端口只返回 404。我如何以在给定端口上为 Angular 应用程序提供服务的方式实际运行应用程序和开发中的一模一样?我是否需要运行 ASP.NET 应用程序,然后手动提供 Angular 应用程序的构建文件?
我的csproj在下面
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<SpaRoot>ClientApp\</SpaRoot>
<SpaProxyServerUrl>https://localhost:44459</SpaProxyServerUrl>
<SpaProxyLaunchCommand>npm start</SpaProxyLaunchCommand>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SpaProxy" Version="6.0.8" />
</ItemGroup>
<ItemGroup>
<!-- Don't publish the SPA source files, but do show them in the project files list -->
<Content Remove="$(SpaRoot)**" />
<None Remove="$(SpaRoot)**" />
<None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
</ItemGroup>
<Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
<!-- Ensure Node.js is installed -->
<Exec Command="node --version" ContinueOnError="true">
<Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
</Exec>
<Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
<Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
</Target>
<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 -- --configuration production" />
<!-- Include the newly-built files in the publish output -->
<ItemGroup>
<DistFiles Include="$(SpaRoot)dist\**; $(SpaRoot)dist-server\**" />
<ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
<RelativePath>wwwroot\%(RecursiveDir)%(FileName)%(Extension)</RelativePath>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</ResolvedFileToPublish>
</ItemGroup>
</Target>
</Project>
【问题讨论】: