在ashx文件中使用Session和QueryString

有三点需要注意:

1.命名空间中要加入using System.Web.SessionState;

2.接口名要加入IRequiresSessionState或IReadOnlySessionState;

3.不管是Session还是QueryString都要通过HttpContext来获取。

具体代码如下:

01 <%@ WebHandler Language="C#" Class="UploadHandler" %>
02   
03 using System;
04 using System.IO;
05 using System.Net;
06 using System.Web;
07 using System.Web.SessionState;
08   
09 public class UploadHandler : IHttpHandler, IRequiresSessionState
10 {
11       
12     public void ProcessRequest (HttpContext context) {
13         context.Response.ContentType = "text/plain";
14         context.Response.Charset = "utf-8";
15   
16         string str1 = context.Session["aaa"].ToString();
17         string str2 = context.Request.QueryString["bbb"].ToString();
18     }
19    
20     public bool IsReusable {
21         get {
22             return false;
23         }
24     }
25   
26 }

 

posted on 2011-08-10 11:08  Mr Lin  阅读(241)  评论(1编辑  收藏  举报

导航