定义一个类来获取项目中的页面环境信息。

在做B/S项目时,经常需要获取一些URL发送过来的参数来实例化一些对象,而一些项目要是比较复杂,那他的各种URL所包含的ID 就会很丰富,如果在每个页面都要获取不同的ID ,那每个页面都要写很多代码,如
    if(Request.QueryString["trialInfo_id"]!=null  && Request.QueryString["trialInfo_id"].ToString() !="")
        trialInfo_id = Request.QueryString["trialInfo_id"].ToString();


这样的语句,而且获取到参数之后,还要判断这个参数的有效性,或者还要从这个参数去获取其他的环境变量,而如果每个页面都写代码去实现,那必然导致代码过于重复,复用性不高。 读了ASP。NET FORUMS 的代码后,了解到里头的解决方案就是 获取当前的 HttpContext.Current 信息,并且给予扩展,把项目中可能用到的参数都写到里面去,当然可以把一些判断逻辑也写到里头。代码如:    

using System;
using System.Web;
using System.Collections;
using AspNetForums.Enumerations;

namespace AspNetForums.Components {

    public class ForumContext {
        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 =  "";
  string checkGuid =  "";
  bool isElite=false ;   // fzuray  是否浏览精华
  bool isSubject = false;  //fzuray  是否为专题

        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  精华
   if(context.Request.QueryString["IsSubject"]!=null && context.Request.QueryString["IsSubject"].ToString()=="True")  isElite=true; // fzuray  专题
        }

        public static ForumContext Current {
            get {
                if (HttpContext.Current == null)
                    return new ForumContext();

                return (ForumContext) HttpContext.Current.Items["ForumContext"];
            }

        }

        // *********************************************************************
        //  GetIntFromQueryString
        //
        /// <summary>
        /// Retrieves a value from the query string and returns it as an int.
        /// </summary>
        // ***********************************************************************/
        public static int GetIntFromQueryString(HttpContext context, string key) {
            int returnValue = -1;
            string queryStringValue;

            // Attempt to get the value from the query string
            //
            queryStringValue = 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
                //
                if (queryStringValue.IndexOf("#") > 0)
                    queryStringValue = queryStringValue.Substring(0, queryStringValue.IndexOf("#"));

                returnValue = Convert.ToInt32(queryStringValue);
            }
            catch {}

            return returnValue;

        }

        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);
        }

        // *********************************************************************
        //  GetForumFromForumLookupTable
        //
        /// <summary>
        /// Attempts to use forum lookup table. Capable of flushing lookup table
        /// </summary>
        // ***********************************************************************/
        public Forum GetForumFromForumLookupTable(int forumID) {
            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("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);
        }

        public static string GetApplicationName (HttpContext context) {
            if (context == null)
                return "";

            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   精华
  public bool IsSubject{get{return this.isSubject;}} //fzuray 专题
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值