實戰 HTTP 處理常式(HTTP Handler) (4)–與Web程式共用Session

來源:互聯網
上載者:User
讓 HTTP 處理常式與當前的Web程式共用Session,實在是簡單到了不能再簡單的地步——只要讓類 MyHandler 實現 System.Web.SessionState.IRequiresSessionState 介面就行了。就像這樣MyHandler.cs
public class MyHandler : System.Web.IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    // 
}

之後我們在 MyHandler 類裡面就可以通過 context.Session 或 直接使用 System.Web.HttpContext.Current.Session 來訪問 Web 應用程式的 Session 了。

下面我們來作一個很簡單的 Demo。它實現的效果和本系列第二篇的效果完全一樣。只不過這次使用的方法不是通過字串參數而是使用 Session 將資訊由Web程式傳遞給 MyHandler。這是完成後的效果

原始碼

MyHandler.cs
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 using System.IO;
 6 using System.Drawing;
 7 using System.Drawing.Imaging;
 8 
 9 namespace mylib.system.web
10 {
11     public class MyHandler : System.Web.IHttpHandler, System.Web.SessionState.IRequiresSessionState
12     {
13         #region IHttpHandler 成員
14 
15         public bool IsReusable
16         {
17             get { return false; }
18         }
19 
20         public static string msg
21         {
22             get { return System.Web.HttpContext.Current.Session["mylib.system.web.MyHandler.msg"] as string; }
23             set { System.Web.HttpContext.Current.Session["mylib.system.web.MyHandler.msg"] = value; }
24         }
25 
26         public void ProcessRequest(System.Web.HttpContext context)
27         { 
28             string s = (msg == null)? "null" : msg;
29 
30             context.Response.ContentType = "text/html";
31             context.Response.Write("<html><body>" + s + "</body></html>");
32         }
33         #endregion
34     }
35 }
36 

Default.aspx
 1 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 2 
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml" >
 6 <head runat="server">
 7     <title>無標題頁</title>
 8 </head>
 9 <body>
10     <form id="form1" runat="server">
11     <div>
12        <iframe src="~/MyHandler.jxd"></iframe>
13     </div>
14     </form>
15 </body>
16 </html>
17 

Default.aspx.cs
 1 using System;
 2 using System.Data;
 3 using System.Configuration;
 4 using System.Web;
 5 using System.Web.Security;
 6 using System.Web.UI;
 7 using System.Web.UI.WebControls;
 8 using System.Web.UI.WebControls.WebParts;
 9 using System.Web.UI.HtmlControls;
10 
11 using System.Collections.Generic;
12 
13 public partial class _Default : System.Web.UI.Page 
14 {
15     protected void Page_Load(object sender, EventArgs e)
16     {
17         mylib.system.web.MyHandler.msg = "impossible is nothing";
18     }
19 }

上面的代碼用了幾個小技巧:

1.  避免 Session 重名。我們的應用程式可能會有許多地方會用到 Session,如果出現重名,就會產生很難捉到的 Bug,所以我們必須使用一種可以有效避免重名的命名規範。在本例中,我們使用的命名規範是“命名空間名+類名+屬性名稱”,見 MyHandler.cs 的第22行。

2. 將對 Session 的訪問封裝到屬性中。為了避免 Session 重名,我們使用了很長的 Session 名稱。這長長的名字即不易讀,也難以書寫。放到代碼中,會使代碼冗長而醜陋。更要命的是,如果萬一需要修改某個 Session 名字,就需要在整個項目中進行尋找和替換,這是一項既乏味又危險的工作。解決方案是,將對 Session 的讀寫封裝到一個靜態屬性中,見 MyHandler.cs 的第20~24行、MyHandler.cs 的第28行、Default.aspx.cs 的第17行。

3. 使用 System.Web.HttpContext.Current.Session 訪問當前Web 應用程式的Session。通常我們是使用 Page.Session(在網頁的後台代碼中)或 context.Session(在 MyHandler 類的 ProcessRequest 函數中)來訪問當前Web應用程式的 Session。但是在類庫中,我們無法擷取 Page 變數或 context 變數,這時我們就可以使用System.Web.HttpContext.Current.Session 訪問當前Web 應用程式的Session,見 MyHandler.cs 的第20~24行。

本篇到此結束,下一篇將利用本篇的代碼,稍加改動,實現不需臨時檔案,直接產生PDF檔案並下載到用戶端。

下載本篇全部原始碼

本系列共6篇文章
    實戰 HTTP 處理常式(HTTP Handler) (6)——條碼隨意打
    實戰 HTTP 處理常式(HTTP Handler) (5)——不用臨時檔案,直接開啟動態產生的檔案
    實戰 HTTP 處理常式(HTTP Handler) (4)——與Web程式共用Session   <- you are here.
    實戰 HTTP 處理常式(HTTP Handler) (3)——動態產生圖片
    實戰 HTTP 處理常式(HTTP Handler) (2)——向HTTP 處理常式傳遞參數   
    實戰 HTTP 處理常式(HTTP Handler) (1)——建立一個最簡單的 HTTP Handler  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.