在一般应用程序中创建Session

默认情况下,一般应用程序类继承了IHttpHandler接口,以允许对Http请求进行编程。IHttpHandler接口定义了一个方法:ProcessRequest(HttpContext context)和一个属性IsReusable。

  context.Session会话状态为只读,如果要对其进行操作,还要实现System.Web.SessionState命名空间下的 IRequiresSessionState接口。IRequiresSessionState接口是一个标记接口,没有任何方法,它使指定目标HTTP 处理程序对会话状态值具有读写访问权。

 


using System;
using System.Web;
using System.Web.SessionState;

public class Test : IHttpHandler,IRequiresSessionState
{
    
    public void ProcessRequest (HttpContext context) 
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");

        context.Session["UserID"= 1;
    }
 
    public bool IsReusable 
    {
        get 
        {
            return false;
        }
    }

}
posted @ 2014-09-03 20:50  小蚕豆  阅读(111)  评论(0编辑  收藏  举报