带有 React 的 ASP.NET Core - 431 请求标头太长

J. *_*Loe 6 asp.net http kestrel-http-server asp.net-core

我有一个以dotnet run命令开头的 dotnet 应用程序。我还有一个 React 应用程序,我从yarn start.

当我打开浏览器localhost:3000(反应应用程序所在的位置)时,服务器日志如下所示:

....this goes on for long
 Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/build/bundle.js  
info: Microsoft.AspNetCore.Server.Kestrel[17]
      Connection id "0HLMFVTO65C53" bad request data: "Request headers too long."
Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Request headers too long.
   at Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException.Throw(RequestRejectionReason reason)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.TakeMessageHeaders(ReadOnlySequence`1 buffer, SequencePosition& consumed, SequencePosition& examined)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.ParseRequest(ReadOnlySequence`1 buffer, SequencePosition& consumed, SequencePosition& examined)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.TryParseRequest(ReadResult result, Boolean& endConnection)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequestsAsync[TContext](IHttpApplication`1 application)
Run Code Online (Sandbox Code Playgroud)

大约 15 秒后页面加载,但我在浏览器控制台中收到关于 bundle.js 431 错误的错误。

如果我使 Kestrel 服务器的 RequestHeaders 最大总大小更大,同样的事情会发生,但这种情况会持续更长时间,最终结果是 500 服务器错误而不是 431。

此外,如果我尝试使用邮递员向服务器发出简单的 DELETE 请求,结果几乎相同。好像请求陷入无限循环,然后返回 431。

其中的几行Startup.cs可能是相关的:

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

// In production, the React files will be served from this directory
services.AddSpaStaticFiles(configuration =>
     {
          configuration.RootPath = "client/build";
     }
);


app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action=Index}/{id?}");
            }
);
app.UseSpaStaticFiles();
app.UseSpa(spa =>
            {
                if (env.IsDevelopment())
                    spa.UseProxyToSpaDevelopmentServer("http://localhost:3000");
            }
);
Run Code Online (Sandbox Code Playgroud)

到底是怎么回事?

Raf*_*afe 3

我遇到了同样的问题,但是使用 Angular,在 localhost 开发期间使用 asp.net core。我的解决方案是在 package.json 文件内的“start”脚本中设置节点的标头大小。

内部脚本json 对象:

"start": "node --max-http-header-size=100000 ./node_modules/@angular/cli/bin/ng serve",
Run Code Online (Sandbox Code Playgroud)