HttpHandler中无法使用Session(context.Session 为Null)
2014-10-23 14:32
381 查看
一.问题描述:
最近需要做一个对特定请求(.report)进行响应的接口,当然是使用实现IHttpHandler来进行处理,实现IHttpHandler接口就必须要实现它的两个方法,ProcessRequest(HttpContext context) 和IsRunable() ,看到在ProcessRequest(HttpContext context) 中有个HttpContext的输入参数,以为通过这个就可以对所有的服务器对象进行使用。不过问题出现了,在这个自定义HTTP响应处理类中,对于Request和Response都可以通过使用HttpContext来引用使用,不过Session 就是不行,总是出现对象未进行引用的错误!
二.解决办法:
通过上网查资料,却无意中发现在自定义HTTPHANDLER中使用SESSION 的方法!
1、先引用System.Web.SessionState这个命名空间,
2、如果是要在HttpHandler中读取Session的内容,就要在实现IHttpHandler的类中同时实现IReadOnlySessionState这个接口。
3、如果是要在HttpHandler中读写Session的内容,就要在实现IHttpHandler的类中同时实现IRequiresSessionState 这样就可以在自定义的HttpHandler 中正常的使用Session了。
三.代码演示:
1.前台页面cs文件
public partial class TestPage: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BtnUpLoad_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(TemplateUpload.FileContent);
Session["upLoadXmlDoc"] = doc;
string url = "test.report";
Response.Redirect(url);
}
}
2.实现IHttpHandler的HttpHandler类,用来处理test.report
public class myHttpHandler:IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
String className = businessConfig.Business[context.Request.Path];
//本行将报错,提示:Object reference not set to an instance of an object.
//添加监视,得知context.Session为null
XmlDocument doc = context.Session["upLoadXmlDoc"]
}
// Override the IsReusable property.
public bool IsReusable
{
get { return true; }
}
}
3.解决办法:
让myHttpHandler再实现IRequiresSessionState就ok了
public class myHttpHandler:IHttpHandler,IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
String className = businessConfig.Business[context.Request.Path];
XmlDocument doc = context.Session["upLoadXmlDoc"];
}
// Override the IsReusable property.
public bool IsReusable
{
get { return true; }
}
}
最近需要做一个对特定请求(.report)进行响应的接口,当然是使用实现IHttpHandler来进行处理,实现IHttpHandler接口就必须要实现它的两个方法,ProcessRequest(HttpContext context) 和IsRunable() ,看到在ProcessRequest(HttpContext context) 中有个HttpContext的输入参数,以为通过这个就可以对所有的服务器对象进行使用。不过问题出现了,在这个自定义HTTP响应处理类中,对于Request和Response都可以通过使用HttpContext来引用使用,不过Session 就是不行,总是出现对象未进行引用的错误!
二.解决办法:
通过上网查资料,却无意中发现在自定义HTTPHANDLER中使用SESSION 的方法!
1、先引用System.Web.SessionState这个命名空间,
2、如果是要在HttpHandler中读取Session的内容,就要在实现IHttpHandler的类中同时实现IReadOnlySessionState这个接口。
3、如果是要在HttpHandler中读写Session的内容,就要在实现IHttpHandler的类中同时实现IRequiresSessionState 这样就可以在自定义的HttpHandler 中正常的使用Session了。
三.代码演示:
1.前台页面cs文件
public partial class TestPage: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BtnUpLoad_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(TemplateUpload.FileContent);
Session["upLoadXmlDoc"] = doc;
string url = "test.report";
Response.Redirect(url);
}
}
2.实现IHttpHandler的HttpHandler类,用来处理test.report
public class myHttpHandler:IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
String className = businessConfig.Business[context.Request.Path];
//本行将报错,提示:Object reference not set to an instance of an object.
//添加监视,得知context.Session为null
XmlDocument doc = context.Session["upLoadXmlDoc"]
}
// Override the IsReusable property.
public bool IsReusable
{
get { return true; }
}
}
3.解决办法:
让myHttpHandler再实现IRequiresSessionState就ok了
public class myHttpHandler:IHttpHandler,IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
String className = businessConfig.Business[context.Request.Path];
XmlDocument doc = context.Session["upLoadXmlDoc"];
}
// Override the IsReusable property.
public bool IsReusable
{
get { return true; }
}
}
相关文章推荐
- HttpHandler中无法使用Session(context.Session 为Null)
- 在自定义HttpHandler中无法使用Session
- 在自定义HttpHandler中无法使用Session[zt]
- 使用shiro的session后,HttpSession.getServletContext() 获取一直是null的解决方法
- 使用HttpHandler出现的无法使用Session的错误及解决
- abp项目中无法使用HttpContext.Current.Session[""]的问题
- 在自定义HttpHandler中无法使用Session
- 在ASP.NET中使用IHttpHandler处理请求(如自实现AJAX)时,无法获得Session(或者说是Session 为 null)的原因及解决方法
- ashx中使用HttpContext.Current.Session ,出现未将对象引用设置到实例
- 在HttpHandlers (ASHX files)中使用Session
- 一般处理程序中 httpContext.current.session为null的解决办法
- 在自定义HttpHandler 中使用Session
- 怎样在自定义HTTPHANDLER中使用SESSION的方法?
- 在自定义HttpHandler中使用Session
- ashx中使用HttpContext.Current.Session ,出现未将对象引用设置到实例
- 在自定义HttpHandler 中使用Session
- 自己定义的httpHandlers无法使用Session(ASP.net 1.1)
- HttpHandler使用Session
- 在自定义HttpHandler中使用Session
- 如何使用单元测试 测试WEB组件方法中的含有Session、Cookie等HttpContext功能调用的方法?[转载]