利用IRequiresSessionState,讓Handler可以取得session的值
前陣子好像有個人問我,在Handler裡如何取得Session的值....
我在網路上找到了答案..分享給大家呀....
我模擬一個實作..給大家看....
c#範例:
handler.aspx
01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="handler.aspx.cs" Inherits="handel" %>
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head id="Head1" runat="server">
06 <title>未命名頁面</title>
07 </head>
08 <body>
09 <form id="form1" runat="server">
10 <div>
11 <a href="Handler.ashx">Handler.ashx</a>
12 </div>
13 </form>
14 </body>
15 </html>
16
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head id="Head1" runat="server">
06 <title>未命名頁面</title>
07 </head>
08 <body>
09 <form id="form1" runat="server">
10 <div>
11 <a href="Handler.ashx">Handler.ashx</a>
12 </div>
13 </form>
14 </body>
15 </html>
16
handler.aspx.cs
01 using System;
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class handel : System.Web.UI.Page
13 {
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 Session["msg"] = "hi..puma";
17 }
18 }
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class handel : System.Web.UI.Page
13 {
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 Session["msg"] = "hi..puma";
17 }
18 }
Handler.ashx
01 <%@ WebHandler Language="C#" Class="Handler" %>
02
03 <%@ WebHandler Language="C#" Class="Handler" %>
04
05 using System;
06 using System.Web;
07 using System.Web.SessionState;
08
09 public class Handler : IHttpHandler, IRequiresSessionState
10 {
11
12 public void ProcessRequest(HttpContext context)
13 {
14 //取得來自上頁的session值
15 string msg = context.Session["msg"].ToString();
16 context.Response.ContentType = "text/html";
17 context.Response.Write(msg);
18 }
19
20 public bool IsReusable
21 {
22 get
23 {
24 return false;
25 }
26 }
27
28 }
02
03 <%@ WebHandler Language="C#" Class="Handler" %>
04
05 using System;
06 using System.Web;
07 using System.Web.SessionState;
08
09 public class Handler : IHttpHandler, IRequiresSessionState
10 {
11
12 public void ProcessRequest(HttpContext context)
13 {
14 //取得來自上頁的session值
15 string msg = context.Session["msg"].ToString();
16 context.Response.ContentType = "text/html";
17 context.Response.Write(msg);
18 }
19
20 public bool IsReusable
21 {
22 get
23 {
24 return false;
25 }
26 }
27
28 }
記得在Handler.ashx裡
using System.Web.SessionState;
與實作IRequiresSessionState