一般处理程序不能使用Session的问题

  在一般处理程序中,直接访问Session是访问不了的,需要添加引用 using System.Web.SessionState; 并且要让类实现IRequiresSessionState 和IReadOnlySessionState这两个接口。

 1 public class CheckUser : IHttpHandler,IRequiresSessionState , IReadOnlySessionState
 2     {
 3 
 4         public void ProcessRequest(HttpContext context)
 5         {
 6             context.Response.ContentType = "text/plain";
 7             string id = context.Request.Params["Name"];
 8             string userName = HttpContext.Current.Session["uname"].ToString();
 9 
10             string strSqlUp = "SELECT UPACCOUNT FROM Calendars WHERE ID='" + id + "'";
11             string UpUser = Convert.ToString(DBHelper.GetScalar(strSqlUp));
12             if (userName.ToLower() == UpUser.ToLower())
13             {
14                 context.Response.Write("1");
15             }
16             else
17             {
18                 context.Response.Write("0");
19             }
20             
21         }
22 
23         public bool IsReusable
24         {
25             get
26             {
27                 return false;
28             }
29         }
30     }
示例代码

  按照这样的方法,你就可以正常使用Session了。

posted @ 2013-07-22 17:34  飘落的枫叶  阅读(306)  评论(0)    收藏  举报