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,就不能做任何修改了。
ASP.NET Core中如果Response.HasStarted已经为true,就不能更改Response.Cookies和Response.Headers等属性的值了的更多相关文章
- ASP.NET Core中显示自定义错误页面-增强版
之前的博文 ASP.NET Core中显示自定义错误页面 中的方法是在项目中硬编码实现的,当有多个项目时,就会造成不同项目之间的重复代码,不可取. 在这篇博文中改用middleware实现,并且放在独 ...
- ASP.NET Core中使用Graylog记录日志
以下基于.NET Core 2.1 定义GrayLog日志记录中间件: 中间件代码: public class GrayLogMiddleware { private readonly Request ...
- ASP.NET Core 中 HttpContext 详解与使用 | Microsoft.AspNetCore.Http 详解 (转载)
“传导体” HttpContext 要理解 HttpContext 是干嘛的,首先,看图 图一 内网访问程序 图二 反向代理访问程序 ASP.NET Core 程序中,Kestrel 是一个基于 li ...
- ASP.NET Core 中 HttpContext 详解与使用 | Microsoft.AspNetCore.Http 详解
笔者没有学 ASP.NET,直接学 ASP.NET Core ,学完 ASP.NET Core MVC 基础后,开始学习 ASP.NET Core 的运行原理.发现应用程序有一个非常主要的 “传导体” ...
- 在ASP.NET Core中使用百度在线编辑器UEditor
在ASP.NET Core中使用百度在线编辑器UEditor 0x00 起因 最近需要一个在线编辑器,之前听人说过百度的UEditor不错,去官网下了一个.不过服务端只有ASP.NET版的,如果是为了 ...
- ASP.NET Core 中文文档 第三章 原理(6)全球化与本地化
原文:Globalization and localization 作者:Rick Anderson.Damien Bowden.Bart Calixto.Nadeem Afana 翻译:谢炀(Kil ...
- ASP.NET Core 中文文档 第三章 原理(13)管理应用程序状态
原文:Managing Application State 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:高嵩 在 ASP.NET Core 中,有多种途径可以对应用程序的状态进行 ...
- 在ASP.NET Core中使用Angular2,以及与Angular2的Token base身份认证
注:下载本文提到的完整代码示例请访问:How to authorization Angular 2 app with asp.net core web api 在ASP.NET Core中使用Angu ...
- 如何在ASP.NET Core中实现CORS跨域
注:下载本文的完整代码示例请访问 > How to enable CORS(Cross-origin resource sharing) in ASP.NET Core 如何在ASP.NET C ...
随机推荐
- 【读书笔记】iOS-开发者证书
虽然使用通配符听起来很赞,但问题是使用这种App ID的应用无法使用苹果的Push Notification服务以及应用内支付服务. 如果你有钱的话,建议单独买一个设备用于开发,将来你可能会安装iOS ...
- 【转】Max2013脚本工具的乱码问题
转自:http://www.cnblogs.com/sitt/archive/2012/11/21/2780481.html 有时一些中文的脚本会在max2013中显示为乱码,是因为max2013将多 ...
- Ubuntu16下配置支持Windows访问的samba共享
一.安装Ubuntu samba服务器 $ sudo apt-get install samba $ sudo apt-get install smbclient # Linux客户端测试用 二.创建 ...
- 分享MYSQL中的各种高可用技术
分享MYSQL中的各种高可用技术 图片和资料来源于姜承尧老师(MYSQL技术内幕作者) mysql高可用各个技术的比较 数据库的可靠指的是数据可靠 数据库可用指的是数据库服务可用 可靠的是数据:例如工 ...
- [20180403]访问dba_autotask_task无输出问题.txt
[20180403]访问dba_autotask_task无输出问题.txt --//链接http://www.itpub.net/thread-1911421-1-1.html的讨论,还没注意原先的 ...
- lua保留n位小数方法
time:2015/04/21 1. string.format() function GetPreciseDecimal(nNum, n) if type(nNum) ~= "number ...
- 50个常用sql语句 网上流行的学生选课表的例子
50个常用sql语句 建表: --学生表tblStudent(编号StuId.姓名StuName.年龄StuAge.性别StuSex) --课程表tblCourse(课程编号CourseId.课程名称 ...
- 【PAT】B1064 朋友数(20 分)
以前写的,逻辑不好,过后再改 #include<stdio.h> #include<algorithm> #include<math.h> using namesp ...
- Appium1.9.1 之 Desired Capabilities 释疑
服务关键字 Desired Capabilities在启动session的时候是必须提供的. Desired Capabilities本质上是以key value字典的方式存放,客户端将这些键值对发给 ...
- 玩转FusionCharts:Y轴数字形式(如去掉K)
玩转FusionCharts:Y轴数字形式(如去掉K) 如果运行FusionCharts带的例子,你会发现FusionCharts表中的数字(通常是Y轴)会带上’k’,也就是如20000,会变成20k ...