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,求解
,IRequiresSessionState
{}要继续IRequiresSessionState接口才可以使用session
IRequiresSessionState 这个接口继承就行吗?
是的,要想在handler里使用session,必须继承此接口,该接口无方法体,因此只加上这个接口就可以了。
private static bool requiresSession = false; public bool IsReusable
{
get { return true; }
} public void ProcessRequest(HttpContext context)
{
if (context.Handler is IRequiresSessionState)
{
requiresSession = true;
}
}我又在代码里加了这段。也报错
但是我的session["uname"] != null这里始终是成立的。奇怪了
这样:
string.IsNullOrEmpty(Convert.Tostring(session[""]))