ERR_INVALID_HTTP_RESPONSE 在 Google Chrome 浏览器中使用 Angular 7 和 ASP.Net Core 2.2

ERR_INVALID_HTTP_RESPONSE using Angular 7 and ASP.Net Core 2.2 in Google Chrome browser

我有一个应用程序使用 Angular 7 作为前端,使用 ASP.Net Core 2.2 作为 API 服务。当我使用 Google Chrome 浏览器发送 POSTPUT 请求时,我收到 ERR_INVALID_HTTP_RESPONSE 错误。

当我将 .Net Core 降级到 2.1 版时,一切正常

另外,当我在 Firefox 中测试我的应用程序时,再次一切正常!

我不知道如何解决这个问题

我正在使用 Google Chrome 版本 71

当我检查 Google Chrome 中的“网络”选项卡时,我发现 Chrome 也向服务器发送预检请求...

以下是“网络”选项卡中记录的内容:

第一个:

Request URL: http://localhost:12346/api/XXXX
Request Method: OPTIONS
Status Code: 204 No Content
Remote Address: [::1]:12346
Referrer Policy: no-referrer-when-downgrade

然后:(导致错误)

Request URL: http://localhost:12346/api/XXXX
Referrer Policy: no-referrer-when-downgrade

如您所见,chrome 没有向服务器发送任何 POST 请求(应该发送)。

如您所想,这可能与 ASP NET Core 2.2 有关。在 GitHub https://github.com/aspnet/AspNetCore/issues/4398

上注册了一个问题

解决方法如下 - 在 startup.cs class 中添加以下代码(我在 Configure 方法中首先保留了它)

app.Use(async (ctx, next) =>
{
  await next();
  if (ctx.Response.StatusCode == 204)
  {
    ctx.Response.ContentLength = 0;
  }
});

一定要跟踪 GitHub 问题。