Asp.net核心和生产中的Angular:未提供Spa服务

问题描述 投票:0回答:1

我的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以及已编译的jscss资产。如果我通过在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";什么似乎配置错​​误,或者下一步我应该调查什么?

asp.net angular production
1个回答
0
投票

文件位置错误。我将文件移动到ClientApp\dist\,这就是中间件要查找的位置。奇怪的配置。

© www.soinside.com 2019 - 2024. All rights reserved.