HttpContext的解释意义

在.ashx中,我们HttpContext这个词,到底是什么意思?下面给大家说说

HttpContext 类:封装有关个别 HTTP 请求的所有 HTTP 特定的信息。

 在处理请求执行链的各个阶段中,会有一个对象在各个对象之间进行传递,也即会保存请求的上下文信息,这个对象就是HttpContext对象。HttpContext封装了ASP.NET要处理的单次请求的所有信息。在请求处理机制建立时,HttpContext类有HttpRuntime对象实例化,接着该对象会经历请求生存期的各个阶段

 

HttpContext的介绍:保持单个用户、单个请求的数据,并且数据只在该请求期间保持。被提供用于保持需要在不同的HttpModules和HttpHandlers之间传递的值。它也可以用于保持某个完整请求的相应信息。

Current属性是个十分有用的静态成员,返回当前请求的HttpContex对象。Items是一个哈希表,在处理请求所涉及的模块和处理程序间共享数据。每个自定义模块或处理程序能够将自身信息添加到请求的HttpContext对象中,在Items中存储的信息最终被页面使用,但这些信息只能在请求的执行期间访问

 

代码如下:

public class AdminAgentDo : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string actype = context.Request.QueryString["actype"];
if(actype=="GetAgentList")
{
int page = int.Parse(context.Request.QueryString["page"]);
int pagesize = int.Parse(context.Request.QueryString["pagesize"]);
string keyword = Microsoft.JScript.GlobalObject.unescape(context.Request.QueryString["keyword"]);  //以上是接受http传来的值
string strPro = "usp_GetRecordFromPage";
string SelectFieldName = "ID,RealName,Province,ContactPho,Notes,AddTime";
string strWhere = "1=1";
if (keyword.Length > 0)
{
//获取关键字,并且将中间多于一个空格的合并为一个空格
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"( )+");
string str = regex.Replace(keyword, " ");
str = str.Replace("]", "] ");
string[] strmember = str.Split(' ');
System.Text.StringBuilder str1 = new System.Text.StringBuilder();
for (int i = 0; i < strmember.Length; i++)
{
str1.Append("and RealName like '%" + strmember[i] + "%' ");
}
str = str1.ToString();
strWhere += " " + str;
}
using (SqlConnection con = new SqlConnection(SqlHelper.ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand(strPro, con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] myparams = {
new SqlParameter("@tblName",SqlDbType.VarChar,100),
new SqlParameter("@SelectFieldName",SqlDbType.VarChar,500),
new SqlParameter("@strWhere",SqlDbType.VarChar,2000),
new SqlParameter("@OrderFieldName",SqlDbType.VarChar,100),
new SqlParameter("@PageSize",SqlDbType.Int),
new SqlParameter("@PageIndex",SqlDbType.Int),
new SqlParameter("@iRowCount",SqlDbType.Int),
new SqlParameter("@OrderType",SqlDbType.Int)
};
myparams[0].Value = "Agent";
myparams[1].Value = SelectFieldName;
myparams[2].Value = strWhere;
myparams[3].Value = "ID";
myparams[4].Value = pagesize;
myparams[5].Value = page;
myparams[6].Direction = ParameterDirection.Output;
myparams[7].Value = 1;
cmd.Parameters.AddRange(myparams);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
int res = Convert.ToInt32(myparams[6].Value);
string DataList = string.Empty;
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
//ID,RealName,Province,ContactPho,Notes,AddTime
DataList += "{\"ID\":" + dr["ID"] + ",\"RealName\":\"" + dr["RealName"] + "\",\"Province\":\"" + dr["Province"] + "\",\"ContactPho\":\""+dr["ContactPho"]+"\",\"Notes\":\""+dr["Notes"]+"\",\"AddTime\":\""+dr["AddTime"]+"\"},";
}
DataList = DataList.Substring(0, DataList.LastIndexOf(','));
}
string val = "{\"Result\":1,\"List\":[" + DataList + "],\"RecordCount\":" + res + "}";
context.Response.Write(val);
}
}
else if(actype=="DELAgent")
{
int id = int.Parse(context.Request.Form["id"]);
AdminAgent adminagentdal = new AdminAgent();
adminagentdal.AdminDeleteAgent(id);
context.Response.Write(1);
}
else if(actype=="DELBatchAgent")
{
string delids = context.Request.Form["delids"];
AdminAgent adminagentdal = new AdminAgent();
adminagentdal.AdminDelBatchAgent(delids);
context.Response.Write(1);  //用Context通过Http再传回去
}
}

 

posted @ 2015-12-24 20:45  最美高级程序员  阅读(2527)  评论(0编辑  收藏  举报