Asp.net核心和Angular投入生产:不提供Spa服务

超人洛佩兹

我的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。另外,您可能希望切换到开发环境。

当我在app-distmacOS开发系统上运行相同的命令时,出现相同的错误(在首次设置后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>

我曾尝试复制角度index.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";
    });
}

中止configuration.RootPath应更改为您希望的路径。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章