首页 新闻 赞助 找找看

ASP.NET Core 项目遭遇问题"The SPA default page middleware could not return the default page '/index.html'"

0
悬赏园豆:50 [已解决问题] 解决于 2020-04-08 22:22

在一个 ASP.NET Core 3.1 + Angular 8.2 的 SPA 项目中,POST 请求一个不存在的路径就会出现下面的错误:

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 Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context)

请问如何解决?

dudu的主页 dudu | 高人七级 | 园豆:31075
提问于:2020-04-07 21:24
< >
分享
最佳答案
1

查看 ASP.NET Core 的源码 SpaDefaultPageMiddleware.cs#L47 知道了异常是在下面的代码中抛出的。

// If we have an Endpoint, then this is a deferred match - just noop.
if (context.GetEndpoint() != null)
{
    return next();
}

var message = "The SPA default page middleware could not return the default page " +
    $"'{options.DefaultPage}' because it was not found, and no other middleware " +
    "handled the request.\n";

后来采用的解决方法

app.MapWhen(
    context => context.Request.Method == HttpMethod.Head.Method || context.Request.Method == HttpMethod.Get.Method,
    frontEndApp =>
    {
        frontEndApp.UseSpa(spa =>
        {
            // ...
        });
    });
dudu | 高人七级 |园豆:31075 | 2020-04-08 22:22

tks!

pccai | 园豆:207 (菜鸟二级) | 2022-07-05 14:06

app.UseWhen(context => HttpMethods.IsGet(context.Request.Method), builder =>
{
builder.UseSpa(spa =>
{
});
});

pccai | 园豆:207 (菜鸟二级) | 2022-07-05 14:12
其他回答(1)
0

The SPA default page middleware could not return the default page '/index.html'

pccai | 园豆:207 (菜鸟二级) | 2022-07-05 14:05
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册