我的 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();