定义一个类来获取项目中的页面环境信息。
2005-01-06 09:56
477 查看
在做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 专题
}
}
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 专题
}
}
相关文章推荐
- jsp页面定义公共变量及项目根目录获取方式
- 一个程序猿成长记:webAppRootKey动态获取项目的运行环境
- servlet下一个页面获取上一个页面的信息
- 利用PHP获取一个页面上的链接信息
- jsp页面获取整个项目的session信息
- Android 如何让EditText不自动获取焦点 在项目中,一进入一个页面, EditText默认就会自动获取焦点。 那么如何取消这个默认行为呢? 在网上找了好久,有点 监听软键盘事件
- Maven项目中通过profile定义使不同环境使用不同配置信息
- 在java环境中有一个专门的获取ttf文件的头信息的Font类
- servlet都有一个servletConfig对象;四个config对象可以调用的方法;ServletContext对象之获取web项目信息;设置全局初始化参数的配置
- 用shell脚本获取一个github项目所有文件的历史信息
- 创建并部署一个Servlet,要求在实现用户登录功能,当用户名和密码正确时跳转到欢迎页面,否则提示出错信息
- Android第三方框架之学习高德地图SDK-----①集成环境,获取定位信息,地图显示。
- 转:从AppStore获取一个iOS App的版本信息
- 获取一个文件的版本信息
- 写一个APP控制第三方播放器播放,以及获取正在播放的歌曲信息
- 微信公众号开发--自定义菜单跳转页面并获取用户信息(续)
- PHP程序开发之使用header定义一个php页面为utf-8编码或GBK编码
- Cocos2dx 3.0开发环境搭建--Eclipse上构建一个Android项目
- 【微信小程序】用户首次进入小程序拒绝授权,如何再次调用授权页面,获取用户信息userInfo
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第二十一节 增加和删除一个IP地址