我的Web应用程序来自dotnet core 3.0 Angular模板。我想将第一个版本部署到生产中,但是我在努力提供Angular应用程序。我正在使用Kestrel作为Web服务器(这是一个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
中运行app-dist
来启动应用程序,则Kestrel将启动,但无法为客户端应用程序提供服务。我可以从wwwroot
以及用于用户身份验证的Razor页面访问次要资产。
在我的Docker部署中,尝试访问https://host/
或https://host/index.html
(以及http
等效项时,出现以下错误:
System.InvalidOperationException:SPA默认页面中间件无法返回默认页面'/index.html',因为找不到它,并且没有其他中间件处理该请求。您的应用程序正在生产模式下运行,因此请确保已发布该应用程序,或者您已手动构建了SPA。另外,您可能希望切换到开发环境。
[当我在macOS开发系统上的app-dist
中运行相同的命令时,出现相同的错误(在首次设置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>
我曾尝试将Angular index.html
和已编译的文件复制到wwwroot
以及ClientApp
文件夹,但没有任何区别。我也尝试设置spa.Options.SourcePath = "ClientApp/dist/app";
。 什么似乎配置错误,或者下一步我应该调查什么?
文件位置错误。我将文件移动到ClientApp\dist\
,这就是中间件要查找的位置。奇怪的配置。