ASP.NET ASHX中访问Session

默认,在ashx文件中无法使用Session,直接获取context.Session只能取得null。

解决办法:

  1. 添加命名空间 using System.Web.SessionState 的引用
  2. 让这个General Handler类实现IRequiresSessionState接口
  3. 然后再用context.Session或HttpContext.Current.Session就能获取Session

另外,几篇非常有含金量的帖子,讨论关于解决flash中,或本地html中,或跨域服务器上的html内使用ajax访问web-service或.ashx时session数据丢失的问题,以及页面重定向导致的session数据丢失问题:

  • http://stackoverflow.com/questions/10751188/get-asp-net-session-id-with-jquery
  • http://stackoverflow.com/questions/14465314/how-to-access-session-in-aspx-that-was-modified-in-ashx
  • http://stackoverflow.com/questions/3732978/session-set-in-ashx-and-get-that-session-on-aspx
  • http://weblogs.asp.net/bleroy/Don_2700_t-redirect-after-setting-a-Session-variable-_2800_or-do-it-right_2900_
原文地址:https://www.cnblogs.com/maizhongfei/p/4031125.html