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

Vah*_*ian 7 google-chrome asp.net-core angular angular7

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

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

此外,当我在Firefox中测试我的应用程序时,一切都运行良好!

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

我使用的是谷歌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
Run Code Online (Sandbox Code Playgroud)

然后:(导致错误)

Request URL: http://localhost:12346/api/XXXX
Referrer Policy: no-referrer-when-downgrade
Run Code Online (Sandbox Code Playgroud)

如您所见,chrome不会向POST服务器发送任何请求(应该发送).

AD8*_*AD8 9

这可能与你想象的ASP NET Core 2.2有关.在GitHub上注册了一个问题https://github.com/aspnet/AspNetCore/issues/4398

解决方法如下 - 在您的startup.cs类中添加以下代码(我在Configure方法中保留了第一个)

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

跟踪GitHub问题.

  • 截至上一篇,修复已合并到ASP.NET Core 2.2.1的SDK中.如果您下载并安装它,则不再需要解决方法. (2认同)