asp.net - 生产中的 Asp.net 核心和 Angular:不提供 Spa
问题描述
我的 Web 应用程序是从 dotnet core 3.0 Angular 模板构建的。我想将第一个版本部署到生产中,但我正在努力提供 Angular 应用程序。我使用 Kestrel 作为网络服务器(它是一个 Intranet 应用程序)。
我已经发布了dotnet publish -c Release -o ../app-dist
构建 Asp.net 应用程序的应用程序,它触发ng
了构建客户端的生产版本。生成的文件夹结构包括一个app-dist/wwwroot
文件夹,其中包含一些资产,例如 favicon,并且它有一个app-dist/ClientApp/dist/app
文件夹,其中包含index.html
和编译的js
资产css
。如果我通过运行dotnet app.dll
in启动应用程序app-dist
,Kestrel 会启动,但它无法为客户端应用程序提供服务。wwwroot
我可以从用于用户身份验证的 Razor 页面访问次要资产。
在我的 docker 部署中,尝试访问https://host/
或https://host/index.html
(以及http
等效项)时出现以下错误:
System.InvalidOperationException:SPA 默认页面中间件无法返回默认页面“/index.html”,因为未找到它,并且没有其他中间件处理该请求。您的应用程序正在生产模式下运行,因此请确保它已发布,或者您已手动构建了 SPA。或者,您可能希望切换到开发环境。
app-dist
当我在我的 macOS 开发系统上运行相同的命令时,我得到相同的错误(在第一次设置之后export ASPNETCORE_ENVIRONMENT=production
)。
在我的Startup.cs
我有以下内容:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseStaticFiles();
if (!env.IsDevelopment())
{
app.UseSpaStaticFiles();
}
...
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
}
在我的 csproj 中:
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
<IsPackable>false</IsPackable>
<SpaRoot>ClientApp\</SpaRoot>
<DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>
<BuildServerSideRenderer>false</BuildServerSideRenderer>
</PropertyGroup>
...
<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' " />
<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>
我曾尝试将 Angularindex.html
和编译文件复制到wwwroot
文件ClientApp
夹中,但都没有任何区别。我也试过设置spa.Options.SourcePath = "ClientApp/dist/app";
。 什么似乎配置不正确,或者我接下来应该调查什么?
解决方案
如果您想在生产环境中更改服务应用程序的目录,可以通过ConfigureServices
以下方式进行:
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
}
Aborveconfiguration.RootPath
应该更改为您希望的路径。
推荐阅读
- r - R中的元素子集
- powershell - Powershell - 从“组对象”输出重命名文件
- ansible - 使用 Ansible 脚本在 Windows 操作系统上的 exe 文件上设置“以管理员身份运行”
- r - R 错误,包 'colorspace',scale_fill_continuous_diverging() 在函数中不起作用,而 scale_color_continuous_diverging() 起作用
- python - 如何在上面的csv文件中和给定值之间保存值在python中自己的csv
- javascript - 使用响应式扩展立即显示子行,包括第一列
- tensorflow - 评估训练和加载的 CNN keras 模型问题
- python - 重新采样时间序列
- python - 无法在 Emacs Shell 中激活 Conda 环境
- get - Shopify API - PUT 请求更新请求成功的订单,但订单未更新