[常见问题]cookie使用1.Page与HttpContext的Request、Response - 中国WEB开发者网络 (http://www.webasp.net) -- 技术教程 (http://www.webasp.net/article/) --- [常见问题]cookie使用1.Page与HttpContext的Request、Response (http://www.webasp.net/article/24/23592.htm) |
| -- 作者:未知 -- 发布日期: 2005-05-09 |
自编程序如下 using System; using System.Web; using System.Web.UI; namespace hnwl.config { /// <summary> /// Cookdef 的摘要说明。 /// cookies相关的定义,以及涉及到cook的一些检验,如用户登录标示 /// </summary> public class Cookdef :Page { private HttpCookie cook; public Cookdef() { // // TODO: 在此处添加构造函数逻辑 // } public void New(string name) { cook=new HttpCookie(name); } public HttpCookie Req(string name) { cook=Request.Cookies[name]; return cook; } public void Append() { Response.Cookies.Add(cook);//运行后提示此行出错 } 编译过程未提示错误,但是运行后出现 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可以在任何情况下使用,可获取当前的上下文数据。 |
| webasp.net |