using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;namespace ORM_Entity
{
   public class IHttpHandlerText:IHttpHandler
    {
       public bool IsReusable
       {
           get { return true; }
       }       public void ProcessRequest(HttpContext context)
       {           HttpResponse Response = context.Response;
           HttpRequest Request = context.Request;           HttpSessionState session = context.Session;
           if (session["uname"] != null && !string.IsNullOrEmpty(session["uname"].ToString()))
           {
               context.Response.ContentType = "text/html";
               context.Response.Write("<html>");
               context.Response.Write("<body>");
               context.Response.Write("<b>Response by HelloHandler!</B>");
               context.Response.Write("</body>");
               context.Response.Write("</html>");
           }
           else
           {
               context.Response.Redirect("~/error/Default.aspx");
           }
           //context.Response.ContentType = "text/html";
           //context.Response.Write("<html>");
           //context.Response.Write("<body>");
           //context.Response.Write("<b>Response by HelloHandler!</B>");
           //context.Response.Write("</body>");
           //context.Response.Write("</html>");
           
       }
    }
}
未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 23:            HttpSessionState session = context.Session;
行 24:            if (session["uname"] != null && !string.IsNullOrEmpty(session["uname"].ToString()))
行 25:            {
行 26:                context.Response.ContentType = "text/html";
行 27:                context.Response.Write("<html>");
 源文件: D:\ORM\ORM_Entity\IHttpHandlerText.cs    行: 25 我在web.config里的配置没有错问题出在这个session,求解

解决方案 »

  1.   

    你要实现一个session接口 session在IhttpHandler中才管用  public class IHttpHandlerText:IHttpHandler
    ,IRequiresSessionState 
      

  2.   

    实现IRequiresSessionState 这个接口 就可以了
      

  3.   

    public class IHttpHandlerText:IHttpHandler,IRequiresSessionState
    {}要继续IRequiresSessionState接口才可以使用session
      

  4.   

    多谢两位
    IRequiresSessionState 这个接口继承就行吗?
      

  5.   


    是的,要想在handler里使用session,必须继承此接口,该接口无方法体,因此只加上这个接口就可以了。
      

  6.   

    还是报错···
            private static bool requiresSession = false;       public bool IsReusable
           {
               get { return true; }
           }       public void ProcessRequest(HttpContext context)
           {
                          if (context.Handler is IRequiresSessionState)
               {
                   requiresSession = true;
               }
    }我又在代码里加了这段。也报错
      

  7.   

    f (session["uname"] != null && !string.IsNullOrEmpty(session["uname"].ToString()))是这里错了。强制转换的时候string.IsNullOrEmpty(session["uname"].ToString())这里报错,去掉就好了
    但是我的session["uname"] != null这里始终是成立的。奇怪了
      

  8.   

    session[]直接toString()是不行的
    这样:
    string.IsNullOrEmpty(Convert.Tostring(session[""]))
      

  9.   

    确实,强制转换的时候,当值为NULL的时候,会抛异常