It is really easy to make the HTTP processing program share the Session with the current Web program. Just let the class MyHandler implement the System. Web. SessionState. IRequiresSessionState interface. Like this, MyHandler. cs
Public class MyHandler: System. Web. IHttpHandler, System. Web. SessionState. IRequiresSessionState
{
//
}
Then we can access the Session of the Web application through context. Session or directly using System. Web. HttpContext. Current. Session in the MyHandler class.
Here is a simple Demo. It achieves the same effect as the second article in this series. However, the method used this time is not a string parameter, but a Session is used to pass the information to MyHandler by a Web program. This is the result after completion
Source code
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 Member
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 ("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 6 7 <title> No title page </title>
8 9 <body>
10 <form id = "form1" runat = "server">
11 <div>
12 <iframe src = "~ /MyHandler. jxd "> </iframe>
13 </div>
14 </form>
15 </body>
16 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}
The above Code uses a few tips:
1. Avoid duplicate Session names. The Session may be used in many places in our applications. If duplicate names occur, a very difficult Bug will occur. Therefore, we must use a naming convention that can effectively avoid duplicate names. In this example, the naming convention we use is "namespace name + class name + attribute name". For details, see row 22nd of MyHandler. cs.
2. encapsulate Session access into attributes. To avoid duplicate Session names, we use a long Session name. This long name is neither easy to read nor to write. Putting it in the Code will make the code lengthy and ugly. Even worse, if you need to modify the name of a Session, you need to search and replace it in the entire project. This is a tedious and dangerous job. The solution is to encapsulate Session read/write into a static attribute. For details, see 20th ~ of MyHandler. cs ~ Rows 24, 28th of MyHandler. cs, and 17th of Default. aspx. cs.
3. Use System. Web. HttpContext. Current. Session to access the Session of the Current Web application. Generally, we use Page. Session (in the background code of the webpage) or context. Session (in the ProcessRequest function of the MyHandler class) to access the Session of the current Web application. However, in the class library, we cannot get the Page variable or context variable. In this case, we can use System. web. httpContext. current. session to access the current Web application Session, see MyHandler. cs 20th ~ 24 rows.
This article ends now. The next article will use the code of this article to slightly modify it to generate a PDF file directly and download it to the client without a temporary file.
Download all source code in this article
6 articles in this series
Practical HTTP Handler (HTTP Handler) (6) -- random bar code
Actual HTTP Handler (HTTP Handler) (5) -- directly open the dynamically generated file without using temporary files
Practice HTTP Handler (4) -- share Session with Web programs<-You are here.
HTTP Handler (3) -- dynamically generate images
HTTP Handler (2) -- passing parameters to the HTTP Handler
HTTP Handler (1) -- create the simplest HTTP Handler