ASP.NET Core中如果Response.HasStarted已经为true,就不能更改Response.Cookies和Response.Headers等属性的值了
最近我在ASP.NET Core中做了一个中间件CustomizedMiddleware,要说该中间件的功能也很简单,其实就是往HttpResponse中添加一个Cookie而已,但是我将添加Cookie的代码放在了next.Invoke(context)的后面,如下所示:
using Microsoft.AspNetCore.Http; using System.Threading.Tasks; namespace Assembly.Middlewares { public class CustomizedMiddleware { private readonly RequestDelegate next; public CustomizedMiddleware(RequestDelegate next) { this.next = next; } public async Task Invoke(Microsoft.AspNetCore.Http.HttpContext context) { //Do something... await next.Invoke(context); //Do something... context.Response.Cookies.Append("DemoCookie", "DemoValue");//在next.Invoke(context)后添加Cookie到Response } } }
结果代码执行到 context.Response.Cookies.Append("DemoCookie", "DemoValue")时,老是抛出异常。
后来查了查资料,原来HttpResponse中有个很重要的属性HasStarted,HttpResponse.HasStarted属性会返回一个bool类型的值,表示当前Http请求的响应(HttpResponse)是否已经把Http头(Header)的内容发送给客户端浏览器了,如果HttpResponse.HasStarted返回true,我们就不能在HttpResponse上更改任何与Http头(Header)相关的内容了,例如Cookies、Headers、StatusCode等都无法做更改了,否则会抛出异常。此外,当HttpResponse.HasStarted返回true时,如果我们调用HttpResponse.Redirect方法进行跳转,这时HttpResponse.Redirect方法也会抛出异常报错:"System.InvalidOperationException: StatusCode cannot be set because the response has already started.",而且HttpResponse.Redirect不会产生任何效果,客户端浏览器页面也不会进行跳转。
结果我发现当上面的代码执行到context.Response.Cookies.Append("DemoCookie", "DemoValue")时,context.Response.HasStarted已经为true了,所以抛出了异常。
因此我将CustomizedMiddleware中间件的代码改为了如下:
using Microsoft.AspNetCore.Http; using System.Threading.Tasks; namespace Assembly.Middlewares { public class CustomizedMiddleware { private readonly RequestDelegate next; public CustomizedMiddleware(RequestDelegate next) { this.next = next; } public async Task Invoke(Microsoft.AspNetCore.Http.HttpContext context) { if (!context.Response.HasStarted)//先判断context.Response.HasStarted { context.Response.Cookies.Append("DemoCookie", "DemoValue");//在next.Invoke(context)前添加Cookie到Response } //Do something... await next.Invoke(context); //Do something... } } }
这次我把context.Response.Cookies.Append("DemoCookie", "DemoValue")放到了next.Invoke(context)的前面,并做了context.Response.HasStarted的判断,只有当context.Response.HasStarted为false时才添加Cookie,这次就没有抛出异常了,而且context.Response.Cookies.Append("DemoCookie", "DemoValue")也成功添加了Cookie到HttpResponse中。
所以这里总结下在ASP.NET Core的中间件中,尽量在next.Invoke(context)调用前做Response.Cookies和Response.Headers等属性的修改,修改前还要判断Response.HasStarted的值,如果是true,就不能做任何修改了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库