From: http://blog.csdn.net/danielhf
In Asp.net, context corresponds to the httpcontext class, which carries information about all the current requests. In the HTTP pipeline, you can obtain request, response, session, and, cache and other useful related objects, especially in httphandler and httpmodule, in the page, we can also access the public attribute session, request, etc, but in fact, it is equivalent to calling through httpcontext.
In addition, httpcontext also has an indexer that returns an idictionary dictionary object, that is, a key-value pair. If you add data when a request is processed, this data set can be shared by all objects during the processing of the current request, including httphandler, httpmodule, and application. After the request ends, this data set will be discarded along with httpcontext. Therefore, you can add data specific to your application here.
There is a forumcontext class in this forum, which belongs to aspnetforums. the components namespace, based on the original httpcontext, encapsulates some environment information related to the application, including fields and methods, to obtain any information related to a specific forum, read the code:
Using system;
Using system. Web;
Using system. collections;
Using aspnetforums. enumerations;
Namespace aspnetforums. COMPONENTS {
Public class forumcontext {
// All information of the current forum. The Initialization is negative to determine whether it is legal in the code.
Int forumid =-1;
Int messageid =-1;
Int forumgroupid =-1;
Int postid =-1;
Int threadid =-1;
Int userid =-1;
String username = "";
Int pageindex =-1;
Int roleid =-1;
String querytext = "";
String returnurl = "";
Httpcontext context;
Datetime requeststarttime = datetime. now;
// This object encapsulates additional information based on the original httpcontext.
Public forumcontext (){
Context = httpcontext. Current;
If (context = NULL)
Return;
// Obtain all Forum-related parameters from the query string, that is, the URL, and fill in the fields.
Postid = getintfromquerystring (context, "postid ");
Forumid = getintfromquerystring (context, "forumid ");
Forumgroupid = getintfromquerystring (context, "forumgroupid ");
Userid = getintfromquerystring (context, "userid ");
Messageid = getintfromquerystring (context, "messageid ");
Pageindex = getintfromquerystring (context, "pageindex ");
Roleid = getintfromquerystring (context, "roleid ");
Querytext = context. Request. querystring ["Q"];
Returnurl = context. Request. querystring ["returnurl"];
}
// If it is null, a new forumcontext object will be created. Otherwise, the forumcontext object will be retrieved from the items set of the current context.
Public static forumcontext current {
Get {
If (httpcontext. Current = NULL)
Return new forumcontext ();
Return (forumcontext) httpcontext. Current. items ["forumcontext"];
// This sentence cannot be understood. Can items store the forumcontext object ??
}
}
// Parse parameters in the request URL
Public static int getintfromquerystring (httpcontext context, string key ){
Int returnvalue =-1;
String querystringvalue;
// Obtain the value of the key from the request
Querystringvalue = context. Request. querystring [Key];
// If this parameter is not provided,-1 is returned, that is, the default value.
If (querystringvalue = NULL)
Return returnvalue;
// Try to convert it into an integer once it is found
Try {
// In some cases, if there is a # In the string, convert the character before # To an integer.
If (querystringvalue. indexof ("#")> 0)
Querystringvalue = querystringvalue. substring (0, querystringvalue. indexof ("#"));
// Otherwise, convert directly
Returnvalue = convert. toint32 (querystringvalue );
}
Catch {}
Return returnvalue;
}
// Redirect to a page that displays the error message. The error message is encapsulated in the corresponding exception object,
// The getsiteurls method returns the siteurls object, which has the path information in the siteurls. XML document,
// Pass the integer corresponding to the predictiontype enumeration as a parameter to the MSG/default. aspx page.
Public static void redirecttomessage (httpcontext context, forumexception exception ){
If (exception. innerexception! = NULL) & (exception. innerexception is forumexception )){
Forumexception inner = (forumexception) exception. innerexception;
}
Context. response. Redirect (globals. getsiteurls (). Message (exception. exceptiontype), true );
}
// Retrieve the hash table corresponding to forumstable from the cache and use forumid to find the corresponding Forum object
Public Forum getforumfromforumlookuptable (INT forumid ){
Forum F = (ForUM) This. forumlookuptable [forumid];
// If it is found, return directly
If (F! = NULL)
Return F;
// If the hash table is not found and is still in the cache, it means it has not expired,
// Clears the cached data and tries to reload the latest
If (F = NULL) & (context. cache ["forumstable"]! = NULL ))
Context. cache. Remove ("forumstable ");
// Try again
F = (ForUM) forumlookuptable [forumid];
// If the reload data is not found, the ID must be wrong.
If (F = NULL ){
Throw new exception ("Forum ID is invalid ");
}
Return F;
}
// Return a hash table. The table key is forumid and the value is a forum object.
Public hashtable forumlookuptable {
// The cache name forumstable corresponds to this hash table. If it has expired, add
// The getforums method returns the hash table, which is put into the buffer and corresponds to the forumstable
// The key value. The lifetime is 120 minutes, and no dependencies exist.
Get {
If (httpruntime. cache ["forumstable"] = NULL)
Httpruntime. cache. insert ("forumstable", forums. getforums (this, 0, true, false), null, datetime. Now. addminutes (120), timespan. Zero );
Return (hashtable) httpruntime. cache ["forumstable"];
}
}
Public static string getapplicationname (){
Return getapplicationname (httpcontext. Current );
}
// Obtain a string such as localhost/sample/test. aspx, host name + virtual directory + Page name
Public static string getapplicationname (httpcontext context ){
If (context = NULL)
Return "";
String hostname = context. Request. url. Host;
String applicationpath = context. Request. applicationpath;
Return hostname + applicationpath;
}
// Return an httpcontext object in singleton Mode
Public httpcontext context {
Get {
If (context = NULL)
Return new httpcontext (null );
Return context;
}
}
Public int messageid {get {return messageid ;}}
Public int forumid {get {return forumid ;}}
Public int forumgroupid {get {return forumgroupid ;}}
Public int postid {get {return postid ;}}
Public int threadid {get {return threadid ;}}
Public int userid {get {return userid ;}}
Public String username {get {return username;} set {username = value ;}}
Public int roleid {get {return roleid ;}}
Public String querytext {get {return querytext ;}}
Public String returnurl {get {return returnurl ;}}
Public int pageindex {get {return (pageindex-1 );}}
Public datetime requeststarttime {get {return requeststarttime ;}}
Public user {get {return users. getuser ();}}
Public sitestatistics statistics {get {return sitestatistics. loadsitestatistics (context, true, 3 );}}
}
}