[转]asp.net Request、Response 响应在此上下文中不可用

  这个问题一般是在页面中使用了Respons.Write()方法,虽然是继承了System.Web.UI.Page。但System.Web.UI.Page下的Response只有在asp.net页面里才有意义,若要使用请用System.Web.HttpContext.Current.Response

 
  编译过程未提示错误,但是运行后出现 System.Web.HttpException: 响应在此上下文中不可用;将Request.Cookies[name]和Response.Cookies.Add(cook)改成HttpContext.Current.Request.Cookies[name]和HttpContext.Current.Response.Cookies.Add(cook)即恢复正常。
  
  解释:Page中的Response,Request只能在页面文件中使用(尽管上文定义的是Cookdef :Page,但只是作为普通类使用),而HttpContext可以在任何情况下使用,可获取当前的上下文数据。
 
 
原文地址:https://www.cnblogs.com/seasons1987/p/3205827.html