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再傳回去
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM