发布.netcore角度项目会显示“ SPA默认页面中间件无法返回index.html”,可在调试中运行

我们继承了一个小型.net核心应用程序,该应用程序具有一个angular 8前端和一个web api后端。当我收到文件时,它们是两个单独的项目,但是为了使其与我们的其他项目相似,并使我们的部署更容易,我们希望将它们组合到一个Visual Studio解决方案/项目中。

我已将文件带入Visual Studio,并做了各种尝试来使项目正常工作。我已经花了大约一天的时间,所以我无法完全告诉您我已经尝试过的内容。目前,当我调试它时,它们都可以正常工作,根本没有问题,一切都很好。但是,如果我发布文件,则会导致加载网站时出现以下错误:

 [Error] An unhandled exception has occurred while executing the request.
System.InvalidOperationException: The SPA default page middleware could not return the default page '/index.html' because it was not found,and no other middleware handled the request.
Your application is running in Production mode,so make sure it has been published,or that you have built your SPA manually. Alternatively you may wish to switch to the Development environment.

   at microsoft.AspNetCore.SpaServices.SpaDefaultPageMiddleware.<>c__DisplayClass0_0.<Attach>b__1(HttpContext context,Func`1 next)
   at microsoft.AspNetCore.Staticfiles.StaticfileMiddleware.Invoke(HttpContext context)
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext,ISwaggerProvider swaggerProvider)
   at microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.Invoke(HttpContext context)
   at microsoft.AspNetCore.Staticfiles.StaticfileMiddleware.Invoke(HttpContext context)
   at microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)

查看已发布的文件,.net核心和Web API文件似乎正确。但是,在ClientApp文件夹中,文件看起来不正确(与Angular 8和WebAPI的另一个项目相比)。

首先,我完全缺少dist文件夹。我相信这样做的原因是未生成dist文件,因为如果我比较两个Visual Studio解决方案(及其项目路径-而不是发布路径);工作项目中包含index.html,runtime.xxx.js和main.xxx.js等文件。非工作项目没有这些文件,而是只有一个out-tsc文件夹,其中包含看起来像是被编译为javascript的打字稿文件的副本的文件夹。

第二,非工作项目的已发布ClientApp文件夹包含angular.json,tsconfig.json和其他两个JSON文件,这些文件不在工作项目的已发布文件中。

这似乎与打字稿文件的发布或生成特别相关,但我不确定如何解决它。

一些可能会增加有价值的细节的东西:

startup.cs的一部分具有以下内容:

services.AddSpaStaticfiles(configuration =>
{
   configuration.RootPath = "ClientApp/dist";
});

// *snip* 

app.UseSpaStaticfiles();

app.UseSpa(spa => {
    spa.Options.SourcePath = "ClientApp";
    if (env.IsDevelopment())
    {
        spa.UseAngularCliServer(npmScript: "start");
    }
});

angular.json的一部分:

  "outputPath": "dist"

完整的tsconfig.json

{
  "compileonSave": false,"compilerOptions": {
    "baseUrl": "./","outDir": "./dist/out-tsc","sourceMap": true,"declaration": false,"downlevelIteration": true,"experimentalDecorators": true,"module": "esnext","moduleResolution": "node","importHelpers": true,"target": "es2015","typeRoots": [
      "node_modules/@types"
    ],"lib": [
      "es2018","dom"
    ]
  },"angularCompilerOptions": {
    "fullTemplateTypeCheck": true,"strictInjectionParameters": true
  }
}

billagg 回答:发布.netcore角度项目会显示“ SPA默认页面中间件无法返回index.html”,可在调试中运行

因此,在逐行比较我的csproj文件之后,我最终将以下内容添加到了无法使用的csproj中。然后,它抱怨另一个小错误(实际上是代码错误),然后一切开始正常工作。

  <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 -- --prod" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:ssr -- --prod" Condition=" '$(BuildServerSideRenderer)' == 'true' " />

    <!-- Include the newly-built files in the publish output -->
    <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>
      </ResolvedFileToPublish>
    </ItemGroup>
  </Target>
,

删除 Angular Api 根中的尾部斜杠

代替

http://localhost:81

我有

http://localhost:81/

我已经回答了这个here

本文链接:https://www.f2er.com/3145987.html

大家都在问