1、System.Web.HttpContext.Current:
一般在web应用程序里,你的程序都是为了处理客户端过来的http请求而执行的,当前正在处理的这个请求的一些上下文信息就保存在一个HttpContext对象里,你通过HttpContext的静态属性Current得到当前这个上下文,然后去取你需要的信息,比如查询字符串等。
2、使用其他的对像如:Request,Response, Session, Application, Cookies对象也可用下面的语句取得:
HttpContext.Current.Request
HttpContext.Current.Response
HttpContext.Current.Session
HttpContext.Current.Session["aaa"]
HttpContext.Current.Application
string cookieheader = (string)HttpContext.Current.Application["cookieheader"];
HttpContext.Current.Cookies
在没有继承page的类中,应该这样写
HttpContext.Current.Server
HttpContext.Current.Server.MapPath
Page p=(Page)HttpContext.Current.Handler;
HttpContext.Current.Response
HttpContext.Current.Request
HttpContext.Current.Session
HttpContext.Current.Cookies
HttpContext.Current.Application
HttpContext.Current.Catche
Cookie的写入:
HttpCookie cookie=new HttpCookie("Simple");
cookie.Values.Add("Simple1",HttpUtility.UrlEncode("大叔,你好!"));
cookie.Values.Add("Simple2","English is OK!Nothing we should do!");
Response.AppendCookie(cookie);
Cookie的读取:
HttpCookie cookie=Request.Cookies["Simple"];
string simple1=HttpUtility.UrlDecode(cookie["Simple1"]);
string simple2=cookie["Simple2"];
这样simple1="大叔,你好!";simple2="English is OK!Nothing we should do!";
3、HttpContext.Current.Response.Write()和Response.Write()有什么区别?
MSDN上解释如下:
HttpContext.Current.Response为当前 HTTP 响应获取 HttpResponse 对象。
Page.Response(也就是第二个)获取与该 Page 对象关联的 HttpResponse 对象。该对象使您得以将 HTTP 响应数据发送到客户端,并包含有关该响应的信息。
通常在类中如果该类不继承System.Web.UI.Page类,可以直接用HttpContext.Current.Response,因为Current是静态属性可以直接使用,而Page则需要实例化.
HttpContext.Current.Response:当前请求的Response对象,在不同的时候这个的返回值是不一样的。
Page.Response:当前页面的Response对象。
如果请求的是同一个页面,它们是一样的。HttpContext.Current.Response能获取到网站当前任何页面正在相应的请求的Response对象,而Page.Response只是当前页面。
对于HttpContext.Current.Response,有可能这一秒是获取到的是甲用户的Response对象,下一秒就是乙用户的Response对象了,它永远是服务器当前处理当前请求的Response对象,这个对象可能是针对不同用户或者不同页面的请求。
而Page.Response永远是处理当前页面时的Response对象
page.response只能在当前页使用
而httpcontext.current.response可在任何地方使用
另外根据根据页面的生命周期:
protected void Page_PreInit(object sender, EventArgs e)
{
Response.Write("Page_PreInit...<br/>");
}
protected void Page_Init(object sender, EventArgs e)
{
Response.Write("Page_Init...<br/>");
}
protected void Page_InitComplete(object sender, EventArgs e)
{
Response.Write("Page_InitComplete...<br/>");
}
protected void Page_PreLoad(object sender, EventArgs e)
{
Response.Write("Page_PreLoad...<br/>");
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Page_Load...<br/>");
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
Response.Write("Page_LoadComplete...<br/>");
}
protected void Page_PreRender(object sender, EventArgs e)
{
Response.Write("Page_PreRender...<br/>");
}
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
Response.Write("Page_PreRenderComplete...<br/>");
}
protected void Page_SaveStateComplete(object sender, EventArgs e)
{
Response.Write("Page_SaveStateComplete...<br/>");
}
protected void Page_Unload(object sender, EventArgs e)
{
//Response.Write("Page_Unload...<br/>");
HttpContext.Current.Response.Write("Page_Unload...<br/>");
}
protected void Page_Disposed(object sender, EventArgs e)
{
}
只有在Disposed以后,页面才被销毁。
那么在Page_Unload的时候,Page对象还是在的,那为什么就不能Page.Response.Write...了呢?答案:Page_Unload:
在卸载阶段,页及其控件已被呈现,因此无法对响应流做进一步更改。如果尝试调用方法(如 Response.Write 方法),则该页将引发异常。
Response.Write()默认是page的.
HttpContext.Current.Response是当前线程上下文件的response对象答案:用Reflector打开看,Page上的Response属性内部存储是_response变量。有一个私有函数ProcessPageCleanup()专门负责在页面逻辑完成时做一些清理工作,其中包括_response = null。在Page的处理主函数,也就是ProcessPage()当中,会在处理完后调用ProcessPageCleanup()。