十二 142008
 

今天使用HttpContext去生成一个XML,想把一个值放在Session中。
于是我是这么做的
context.Session[“userid”]=userid;
报NullReferenceException,奇怪了,context.Session为空?
搜索了一下,原来要继承System.Web.SessionState.IRequiresSessionState这个接口,问题解决,呵呵:)
PS:As和C#代码切换着写还真是难免会写混淆。-_-!

Code
using System;
using System.Web;
using System.Web.SessionState;
public class BuildXml: IHttpHandler, IRequiresSessionState
{
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        try
        {
            context.Session["userid"] = 1;
        }
        catch (Exception ex)
        {
            context.Response.Write(ex.Message);
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>