0

我的 AspNetCore/React 项目在开发环境中运行良好,但只要我在任何其他环境中运行它,我就会得到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.

startup.cs 配置方法的结尾是这样的

      app.UseSpa(spa =>
      {
        spa.Options.SourcePath = "ClientApp";

        if (env.IsDevelopment())
        {
          spa.UseReactDevelopmentServer(npmScript: "start");
        }
      });

但由于这与应用程序中的相应代码相同,因此dotnet new react我想其他的东西会扰乱它。

网络服务器确实启动了,我可以访问 Swagger 页面。只是找不到合适的应用程序。破解上面的代码以便spa.UseReactDevelopmentServer(npmScript: "start");运行会导致在所有环境中找到应用程序(加载环境特定的配置),但由 React 开发服务器提供服务。

我会这条线

spa.Options.SourcePath = "ClientApp";

是什么决定了/index.html和朋友的发现,但有些东西阻止了他们被发现。

我还应该看什么?

我已经看到了这个SPA 默认页面中间件无法返回默认页面,但我认为这不是我面临的问题。

对该项目的搜索显示只有一个文件index.html,它位于ClientApp/public. Configure 方法是这样开始的

  if (env.IsDevelopment())
  {
    app.UseDeveloperExceptionPage();
  }
  else
  {
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days...
    app.UseHsts();
  }

  app.UseHttpsRedirection();
  app.UseStaticFiles();
  app.UseSpaStaticFiles();
4

1 回答 1

2

React资产dotnet build不是由dotnet run.-c Release

还要构建您需要的 React 资产dotnet publish

由于较早使用dotnet publish. 在这种情况下,它们可能是陈旧的。因此,对于除 dev 之外的任何环境,您必须使用dotnet publish.

为什么不dotnet build构建dotnet runReact 资产?在开发中,您不需要它们,因为您正在使用开发服务器进行热交换。在生产中,您几乎肯定会使用由dotnet publish.

为什么我不记得这个?我几乎从不为开发以外的环境进行构建。我不是为生产而构建的,CICD 是这样做的。专业测试人员比程序员更容易意识到这一点,因为他们是为开发以外的环境构建的。

于 2021-04-28T06:22:32.713 回答