引用 AspNetCoreRateLimit => StatusCode cannot be set because the response has already started.

简介: 引用 AspNetCoreRateLimit => StatusCode cannot be set because the response has already started.

app.UseIpRateLimiting(); #需要放在前面,否则抓去不准,还有可能会出现下列错误

本次出现这个错误,是因为 .Net Core 跨域

里面的这行:httpContext.Response.StatusCode = 204; 由于StatusCode 先给它设了 204,所以导致 IpRateLimit 报了下图的错误。

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseIpRateLimiting(); #需要放在前面
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseStatusCodePagesWithReExecute("/Home/Error");
    }
    
    //..............
}
app.UseIpRateLimiting(); #需要放在前面,否则抓去不准,还有可能会出现下列错误

 

 

 

fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HM084E4TPLBN", Request id "0HM084E4TPLBN:00000001": An unhandled exception was thrown by the application.
System.InvalidOperationException: StatusCode cannot be set because the response has already started.
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ThrowResponseAlreadyStartedException(String value)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.set_StatusCode(Int32 value)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.Microsoft.AspNetCore.Http.Features.IHttpResponseFeature.set_StatusCode(Int32 value)
   at Microsoft.AspNetCore.Http.DefaultHttpResponse.set_StatusCode(Int32 value)
   at Microsoft.AspNetCore.Routing.Matching.HttpMethodMatcherPolicy.<>c__DisplayClass15_0.<CreateRejectionEndpoint>b__0(HttpContext context)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at AspNetCoreRateLimit.RateLimitMiddleware`1.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.HandleException(HttpContext context, ExceptionDispatchInfo edi)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

 

 

 

目录
相关文章
|
1月前
|
JSON Java 关系型数据库
Java更新数据库报错:Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
在Java中,使用mybatis-plus更新实体类对象到mysql,其中一个字段对应数据库中json数据类型,更新时报错:Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
44 4
Java更新数据库报错:Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
|
NoSQL MongoDB
启动mongodb报错 Failed to set up listener: SocketException: Address already in use 解决办法
启动mongodb报错 Failed to set up listener: SocketException: Address already in use 解决办法
1160 1
|
7月前
|
资源调度 前端开发
编译第三方前端项目时候出现Syntax Error: TypeError: Cannot set properties of undefined (setting ‘parent‘)
编译第三方前端项目时候出现Syntax Error: TypeError: Cannot set properties of undefined (setting ‘parent‘)
498 0
|
4月前
|
JavaScript 前端开发
15 Uncaught TypeError: Cannot set properties of null (setting ‘onclick‘)
这篇文章解释了在HTML文档中因JavaScript代码在页面元素加载之前执行导致的"Cannot set properties of null (setting ‘onclick’)"错误,并提供了将JavaScript代码置于`<body>`标签内或使用`window.onload`事件确保DOM完全加载后再绑定事件处理器的解决办法。
15 Uncaught TypeError: Cannot set properties of null (setting ‘onclick‘)
TypeError: Cannot set properties of undefined (setting ‘resdata‘),res定义数据出现的问题,定义的方法用this换成that
TypeError: Cannot set properties of undefined (setting ‘resdata‘),res定义数据出现的问题,定义的方法用this换成that
|
JavaScript API
【Vue】Cannot set reactive property on undefined,null,or primitive value:undefined
【Vue】Cannot set reactive property on undefined,null,or primitive value:undefined
390 0
|
7月前
|
开发工具
百度搜索:蓝易云【使用vim编辑器,进行保存时报错:E382: Cannot write, ‘buftype‘ option is set详解。】
请注意,'buftype'选项的设置通常是由于某些插件或配置文件导致的。如果您在Vim的配置文件(如.vimrc)或使用的插件中设置了'buftype'选项,请检查相关配置并确保设置正确。
106 0
|
分布式计算 Hadoop 大数据
|
JavaScript
关于js报错Cannot set properties of undefined (setting ‘innerHTML‘)的问题
关于js报错Cannot set properties of undefined (setting ‘innerHTML‘)的问题
427 0
|
消息中间件 Kafka
Cannot set the value of read-only property ‘additionalSourceDirs‘ for task ‘:jacocoRootReport‘ of
这个问题是gradle的build版本问题,我是在build kafka的老版本时报的错,这个问题我查了一遍网上的内容,发现很多博客忽略了IDEA settings关于gradle的build的一个配置。
521 0
Cannot set the value of read-only property ‘additionalSourceDirs‘ for task ‘:jacocoRootReport‘ of