Define a class to get page environment information in your project.

xiaoxiao2021-03-06  42

When doing the B / S project, you often need to obtain some of the parameters sent by the URL to instantiate some objects, and some projects are more complicated, and the IDs of his various URLs will be very rich, if at each page To get a different ID, then you have to write a lot of code, such as if (Request.QueryString ["TriaLinfo_ID"]! = Null && request.queryString ["trialinfo_id"]. TOSTRING ()! = "") TriaLinfo_ID = Request.QueryString ["TriaLinfo_ID"]. TOSTRING (); After obtaining the parameters, it is necessary to determine the validity of this parameter, or from this parameter to get other environment variables, and if each The page writes code to implement, it will inevitably cause the code to be too repeated, and the reuse is not high. Read the ASP. After the Net Forum code, the solution to the inside is to get the current httpContext.current information, and give an extension, write the parameters that may be used in the project, and of course, some judgment logic can also be written. Codes such as Using System; Use System.collections; Using System.collections; Using ASPNETforums.Enumertions

Namespace aspnetforums.components {

Public class forumContext {int forumid = -1; int forumgroupid = -1; int posid = -1; int threadid = -1; int useerid = -1; string username = ""; int pageindex = 1; int RoleID = -1; string querytext = ""; string returnurl = "; string checkguid =" "; bool iSelite = false; // fzuray browsing the essence Bool Issubject = false; // fzuray is the topic

HttpContext Context; DateTime RequestStartTime = DATETIME.NOW;

Public forumContext () {

CONTEXT = httpContext.current;

IF (context == null) return;

// Read common values ​​we expect to find on the QS // 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 "]; checkGuid = context.Request.QueryString [" guid "]; if (context.Request.QueryString ["! IsElite "] = null && context.Request. QueryString ["iSelite"]. TOSTRING () == "true") iSelite = true; // Fzuray Essence IF (Context.Request.QueryString ["iesseubject"]! = Null && context.request.QueryString ["iesssubject"] .Tostring () == "true") iSelite = true; // fzuray special} public ST Atic forumContext current {get {if (httpcontext.current == null) return new forumContext ();

Return (ForumContext) httpContext.current.items ["forumContext"];

}

// ******************************************************** ********************* // GetIntFromQueryString // ///

/// Retrieves a value from the query string and returns it as an int. / // // ************************************************************ ******************************************* {int ReturnValue = -1; string queryStringValue ; // attempt to get the value from the query = context.request.querystring [key];

// if We Didn't Find Anything, Just Return // IF (QueryStringValue == NULL) Return ReturnValue;

// Found a value, Attempt to Conver to integer // try {

// Special Case if We Find A # in the value //iff (QueryStringValue.indexof ("#")> 0) QueryStringValue = queryStringValue.substring (0, querystringvalue.indexof ("#"));

ReturnValue = Convert.Toint32 (QueryStringValue);} catch {}

Return ReturnValue;

}

Public Static Void RedirectTMessage (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);

// ******************************************************** ********************* // getforumfromforumlookuptable // //

/// Attempts to use forum lookTable. Capable of flushing lookup Table /// // ************************************************************ ********************************************** / Public Forum GetforumFromForumlookuptable (int forum) {forum f = (forum) this.forumlookuptable [forumID]; if (f! = null) return f;

// null out the cached list and attempt to reload // if ((f == null) && (context.cache ["forumstable"]! = Null)) Context.cache.Remove ("forumstable");

f = (forum) forumlookuptable [forumID];

IF (f == null) {throw new exception ("forum id is invalid");}

Return f;

Public HashTable forUMlookuptable {

Get {

IF (httpruntime.cache ["forumstable"] == null) httpruntime.cache.insert ("Forstable", Forums.getforumums (this, 0, true, false), null, datetime.now.addminutes (120), TimeSpan. ZERO);

Return (havehtable) httpruntime.cache ["forumstable"];

}

Public static string getApplicationName () {return getApplicationName (httpContext.current);}

Public Static String getApplicationName (httpContext context) {if (context == null) returnire ";

String hostname = context.request.url.host; string applicationpath = context.request.applicationpath;

Return Hostname ApplicationPath;

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 User {get {return Users.GetUser ();}} public string CheckGuid {get {return checkGuid;}} public SiteStatistics Statistics {get {return SiteStatistics.LoadSiteStatistics (context, true, 3);}} public bool IsElite {get {return this.isElite; }}} // Fzuray Essence public Bool Issubject {Get {Return this.issubject;}} // fzuray special}}

转载请注明原文地址:https://www.9cbs.com/read-72554.html

New Post(0)