首先来看HttpContext类:
System.Web.HttpContext类继承自System.Object,按类名来理解,即是Http上下文类.
此类封装了有关单个HTTP 请求的所有HTTP 特定的信息。此类为继承 IHttpModule 和 IHttpHandler 接口的类提供了对当前 HTTP 请求的 HttpContext 对象的引用。该对象提供对请求的内部 Request、 Response 和 Server 对象的访问。
HttpContext类的常用公共属性有:
Application,为当前 HTTP 请求获取 HttpApplicationState 对象。
Current, 为当前 HTTP 请求获取 HttpContext 对象。
Handler, 为当前 HTTP 请求获取或设置 IHttpHandler 对象。
Items,获取可用于在 HTTP 请求过程中在 IHttpModule 和 IHttpHandler 之间组织和共享数据的键值集合。
Request,为当前 HTTP 请求获取 HttpRequest 对象。
Response,为当前 HTTP 响应获取 HttpResponse 对象。
Server, 获取提供用于处理 Web 请求的方法的 HttpServerUtility 对象。
Session, 为当前 HTTP 请求获取 HttpSessionState 实例。
通过Page类的Context属性可以获得当前的System.Web.HttpContext对象
接着来看Server.Transer()方法:
通过Page类的Server属性类可以Transfer到另一个页面,如Server.Transfer("NewPage.aspx"),可以跳转到新页面中,
使用Server.Transfer()跳转页面,客户端的URL并不会改变,只是在服务器端执行新页并输出,因此可以在新页面中通过获取来获得请求页面传递的 对象和 表单数据及 查询字符串.
假定当前页面为FormerPage.aspx(类名为FormerPage), 跳转到的新页面为NewPage.aspx
从FormerPage.aspx跳转的代码如下:
在新页面(NewPage.aspx)Page_Load()事件中通过如下代码获得传递的数据:
if ( ! IsPostBack)
{
try
{
FormerPage former = (FormerPage)Context.Handler;
txtFromParentPage.Text = former.ClassName; //获取FormerPage中定义的ClassName公共属性
//获取在FormerPage中的上下文字典中添加的ArrayList: Context.Items["FormerPageList1"]
//获取Contex字典项并强制转换类型:
ArrayList list = Context.Items["FormerPageList1"] as ArrayList;
DataSet ds = former.GetDataSet(); //调用FormerPage中定义的GetDataSet()公共方法
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
catch
{
Response.Write("Error get data from parent transfer page!");
}
}
注意上面通过Context.Hander属性来获取当前Http请求的IHttpHander对象,并强制转换成FormerPage对象:
后面可以直接调用这个类的公共属性,方法.同时可以调用在FormerPage中添加的Context字典项(Dictionary Item).
值得注意的是,使用Server.Transer传递页面数据,并使用Context.Handler来接收数据,只有在页面首次加载时,才可以正确获取上一页面的实例,而在postback时,获取的就会是当前页面的实例.
比如在NewPage.aspx中,首次加载时可以获取FomerPage对象,而在回发时尝试获取FormerPage会抛出异常,因为回发时,请求页面已经发生改变,不再是FormerPage发出的请求,而是自身NewPage发出的请求.我们可以在Page_Load()中加入如下代码判断Http请求是由哪个页面发出的:
另外,Server.Transer()有一个重载的方法Server.Trasfer(string newpage,bool preserveForm), 第二个参数用来指定是否保留HttpRequest.Form和HttpRequest.QueryString集合,若为true, 则原页面的Form和QueryString在新的页面中依然有效,可以被调用. 如:
About Context:
Context is an object of type System.Web.HttpContext. It is exposed as a property of the ASP.NET Page class. It's also available from user controls and your business objects (more on that later). Here's a partial list of the objects rolled up by HttpContext:
Reference Articles:
1.MSDN, members of HttpContext, A Matter of Context
2.http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21015699.html
3.http://www.dotnet247.com/247reference/msgs/7/36862.aspx
4.http://www.jaron.cn/chs_scripts/9/2004-09/20040927002606-101590.html
System.Web.HttpContext类继承自System.Object,按类名来理解,即是Http上下文类.
此类封装了有关单个HTTP 请求的所有HTTP 特定的信息。此类为继承 IHttpModule 和 IHttpHandler 接口的类提供了对当前 HTTP 请求的 HttpContext 对象的引用。该对象提供对请求的内部 Request、 Response 和 Server 对象的访问。
HttpContext类的常用公共属性有:
Application,为当前 HTTP 请求获取 HttpApplicationState 对象。
Current, 为当前 HTTP 请求获取 HttpContext 对象。
Handler, 为当前 HTTP 请求获取或设置 IHttpHandler 对象。
Items,获取可用于在 HTTP 请求过程中在 IHttpModule 和 IHttpHandler 之间组织和共享数据的键值集合。
Request,为当前 HTTP 请求获取 HttpRequest 对象。
Response,为当前 HTTP 响应获取 HttpResponse 对象。
Server, 获取提供用于处理 Web 请求的方法的 HttpServerUtility 对象。
Session, 为当前 HTTP 请求获取 HttpSessionState 实例。
通过Page类的Context属性可以获得当前的System.Web.HttpContext对象
接着来看Server.Transer()方法:
通过Page类的Server属性类可以Transfer到另一个页面,如Server.Transfer("NewPage.aspx"),可以跳转到新页面中,
使用Server.Transfer()跳转页面,客户端的URL并不会改变,只是在服务器端执行新页并输出,因此可以在新页面中通过获取来获得请求页面传递的 对象和 表单数据及 查询字符串.
假定当前页面为FormerPage.aspx(类名为FormerPage), 跳转到的新页面为NewPage.aspx
从FormerPage.aspx跳转的代码如下:
private
void
btnToNewPage_Click(
object
sender, System.EventArgs e)
{
ArrayList list = new ArrayList(3);
list.Add("This list ");
list.Add("is for ");
list.Add("FormerPage to see.");
Context.Items["FormerPageList1"] = list;
Server.Transfer("NewPage.aspx");
}
{
ArrayList list = new ArrayList(3);
list.Add("This list ");
list.Add("is for ");
list.Add("FormerPage to see.");
Context.Items["FormerPageList1"] = list;
Server.Transfer("NewPage.aspx");
}
在新页面(NewPage.aspx)Page_Load()事件中通过如下代码获得传递的数据:
if ( ! IsPostBack)
{
try
{
FormerPage former = (FormerPage)Context.Handler;
txtFromParentPage.Text = former.ClassName; //获取FormerPage中定义的ClassName公共属性
//获取在FormerPage中的上下文字典中添加的ArrayList: Context.Items["FormerPageList1"]
//获取Contex字典项并强制转换类型:
ArrayList list = Context.Items["FormerPageList1"] as ArrayList;
DataSet ds = former.GetDataSet(); //调用FormerPage中定义的GetDataSet()公共方法
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
catch
{
Response.Write("Error get data from parent transfer page!");
}
}
注意上面通过Context.Hander属性来获取当前Http请求的IHttpHander对象,并强制转换成FormerPage对象:
FormerPage former
=
(FormerPage)Context.Handler;
值得注意的是,使用Server.Transer传递页面数据,并使用Context.Handler来接收数据,只有在页面首次加载时,才可以正确获取上一页面的实例,而在postback时,获取的就会是当前页面的实例.
比如在NewPage.aspx中,首次加载时可以获取FomerPage对象,而在回发时尝试获取FormerPage会抛出异常,因为回发时,请求页面已经发生改变,不再是FormerPage发出的请求,而是自身NewPage发出的请求.我们可以在Page_Load()中加入如下代码判断Http请求是由哪个页面发出的:
string
path
=
Context.Request.Path;
Response.Write( " <script>alert('Request from: " + path + " ');</script> " );
Response.Write( " <script>alert('Request from: " + path + " ');</script> " );
另外,Server.Transer()有一个重载的方法Server.Trasfer(string newpage,bool preserveForm), 第二个参数用来指定是否保留HttpRequest.Form和HttpRequest.QueryString集合,若为true, 则原页面的Form和QueryString在新的页面中依然有效,可以被调用. 如:
string
str
=
"
Value of Textbox:
"
+
Request.Form[
"
TextBox1
"
]
+
"
<br>
"
;
About Context:
Context is an object of type System.Web.HttpContext. It is exposed as a property of the ASP.NET Page class. It's also available from user controls and your business objects (more on that later). Here's a partial list of the objects rolled up by HttpContext:
Object | Description |
---|---|
Application | A key/value pair collection of values that is accessible by every user of the application. Application is of type System.Web.HttpApplicationState. |
ApplicationInstance | The actual running application, which exposes some request processing events. These events are handled in Global.asax, or an HttpHandler or HttpModule. |
Cache | The ASP.NET Cache object, which provides programmatic access to the cache. Rob Howard's ASP.NET Caching column provides a good introduction to caching. |
Error | The first error (if any) encountered while processing the page. See Rob's Exception to the Rule, Part1 for more information. |
Items | A key-value pair collection that you can use to pass information between all of the components that participate in the processing of a single request. Items is of type System.Collections.IDictionary. |
Request | Information about the HTTP request, including browser information, cookies, and values passed in a form or on the query string. Request is of type System.Web.HttpRequest. |
Response | Settings and content for creating the HTTP response. Request is of type System.Web.HttpResponse. |
Server | Server is a utility class with several useful helper methods, including Server.Execute(), Server.MapPath(), and Server.HtmlEncode(). Server is an object of type System.Web.HttpServerUtility. |
Session | A key/value pair collection of values that are accessible by a single user of the application. Application is of type System.Web.HttpSessionState. |
Trace | The ASP.NET Trace object, which provides access to tracing functionality. See Rob's Tracing article for more information. |
User | The security context of the current user, if authenticated. Context.User.Identity is the user's name. User is an object of type System.Security.Principal.IPrincipal. |
Reference Articles:
1.MSDN, members of HttpContext, A Matter of Context
2.http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21015699.html
3.http://www.dotnet247.com/247reference/msgs/7/36862.aspx
4.http://www.jaron.cn/chs_scripts/9/2004-09/20040927002606-101590.html