1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using PPT_DAL;
  6. namespace PPT_Web.tool
  7. {
  8. /// <summary>
  9. /// Login 的摘要说明
  10. /// </summary>
  11. public class Login : IHttpHandler
  12. {
  13.  
  14. public void ProcessRequest(HttpContext context)
  15. {
  16. context.Response.ContentType = "text/html";
  17. string lo_usm = context.Request.Params["lo_usm"];
  18. string lo_pwd = context.Request.Params["lo_pwd"];
  19. UserEntity userModel = PPT_BLL.UserManager.Login(lo_usm,lo_pwd);
  20. if (userModel != null)
  21. {
  22. context.Session["UserId"] = userModel.USR_ID;
  23. context.Response.Write("{'result':'1'}");
  24. }
  25. else
  26. {
  27. context.Response.Write("{'result':'0'}");
  28. }
  29. }
  30.  
  31. public bool IsReusable
  32. {
  33. get
  34. {
  35. return false;
  36. }
  37. }
  38. }
  39. }

问题:

  如题所示,在一般处理程序文件,即ashx文件下调用Session["UserId"],当调试的时候会显示“未将对象实例化的”的错误

解决方法:

  将该类实现“IRequiresSessionState”的接口即可,而此接口在System.Web.SessionState命名空间下,所以要在上面添加一句:using System.Web.SessionState;

  最终实现代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using PPT_DAL;
  6. using System.Web.SessionState;
  7. namespace PPT_Web.tool
  8. {
  9. /// <summary>
  10. /// Login 的摘要说明
  11. /// </summary>
  12. public class Login : IHttpHandler, IRequiresSessionState
  13. {
  14.  
  15. public void ProcessRequest(HttpContext context)
  16. {
  17. context.Response.ContentType = "text/html";
  18. string lo_usm = context.Request.Params["lo_usm"];
  19. string lo_pwd = context.Request.Params["lo_pwd"];
  20. UserEntity userModel = PPT_BLL.UserManager.Login(lo_usm,lo_pwd);
  21. if (userModel != null)
  22. {
  23. context.Session["UserId"] = userModel.USR_ID;
  24. context.Response.Write("{'result':'1'}");
  25. }
  26. else
  27. {
  28. context.Response.Write("{'result':'0'}");
  29. }
  30. }
  31.  
  32. public bool IsReusable
  33. {
  34. get
  35. {
  36. return false;
  37. }
  38. }
  39. }
  40. }

解决ashx文件下的Session“未将对象引用设置到对象的实例”的更多相关文章

  1. 2014-08-26 解决HttpContext.Current.Session在ashx文件中出现“未将对象引用设置到对象的实例”的问题

    今天是在吾索实习的第35天. 最近在使用HttpContext.Current.Session来获取Session["..."]的值时,常常会弹出错误——“未将对象引用设置到对象的 ...

  2. ashx文件中使用session提示“未将对象引用设置到对象的实例”

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data;u ...

  3. C#一般处理程序设置和读取session(session报错“未将对象引用设置到对象的实例”解决)

    登陆模块时,用到了session和cookie.在一般处理程序中处理session,一直报错.最后找到问题原因是需要调用 irequiressessionstate接口. 在ashx文件中,设置ses ...

  4. 解决:getWeatherbyCityName(city),服务器无法处理请求。 ---> 未将对象引用设置到对象的实例。

    原文:getWeatherbyCityName(city),服务器无法处理请求. ---> 未将对象引用设置到对象的实例. 解决方法:不要直接使用 “服务引用” , 添加为 “Web 引用” 最 ...

  5. 解决使用DevExpress开发错误:未将对象引用设置到对象的实例

    在使用DevExpress是总是会出现一些状况.这次同事在他的机器上调试完成的代码发过来,却出现"未将对象引用设置到对象的实例"的错误,提示是Resources.resx的问题.另 ...

  6. asp.net 中Session的运用,及抛出错误“未将对象引用设置到对象的实例”

    1. 页面载入后,必须要等到page_Load方法执行建立 page对象后才可以使用Session 2. 在.aspx和.cs文件中使用Session的区别 (1).aspx: Session[&qu ...

  7. 一般处理程序中使用Session出现未将对象引用设置到对象的实例

    遇到问题:未将对象引用设置到对象的实例 那就在你的一般处理程序中加入红色背景的代码吧 using System; using System.Collections.Generic; using Sys ...

  8. HttpContext.Current.Session[strName]未将对象引用设置到对象的实例

    项目开发是在4.5.1上,不知道为啥客户提供的服务器上安装的是4.5,差别不大也没去升级,然后部署MVC的时候web.config报错 <system.web> <compilati ...

  9. SQL Sever无法打开链接对话框,未将对象引用设置到对象的实例。(AppIDPackage)

    前几天刚做完系统,先装的是SQL Sever2008,装完后还试了一下,OK~没问题,然后就继续装VS2012等一些软件.搞到很晚没有继续试试就睡了,第二天运行SSMS出问题了..(如图 1.0 所示 ...

随机推荐

  1. C#连接SQLite数据库方法

    --结合Enterprise Library连接,操作SQLite 企业库是我们常用的框架之一,可以从http://entlib.codeplex.com/下载Enterprise Library 5 ...

  2. acdream 1738 世风日下的哗啦啦族I 分块

    世风日下的哗啦啦族I Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acdream.info/problem?pid=1738 Descri ...

  3. 最长公共子序列(LCS问题)

    先简单介绍下什么是最长公共子序列问题,其实问题很直白,假设两个序列X,Y,X的值是ACBDDCB,Y的值是BBDC,那么XY的最长公共子序列就是BDC.这里解决的问题就是需要一种算法可以快速的计算出这 ...

  4. dsPIC33EP 高速PWM模块初始化设置及应用

    //文件 p33pwm6.h #ifndef _P33PWM6_H_ #define _P33PWM6_H_ //#include "p33pwm6.h" #define FSYN ...

  5. iOS开发——多线程OC篇&多线程总结

    多线程总结 //1.NSThread /** 优点:NSThread 比其他两个轻量级. 缺点:需要自己管理线程的生命周期,线程同步,线程同步时对数据的加锁会有一定的系统开销. cocoa给我提供了两 ...

  6. iOS开发——动画编程Swift篇&(四)CABasicAnimation动画

    CABasicAnimation动画 //CABasicAnimation-不透明度 @IBAction func cabOpacity() { let animation = CABasicAnim ...

  7. 云服务器 ECS Linux 磁盘空间满(含 innode 满)问题排查方法

    问题描述 在云服务器 ECS Linux 系统内创建文件时,出现类似如下空间不足提示: No space left on device … 问题原因 导致该问题的可能原因包括: 磁盘分区空间使用率达到 ...

  8. archlinux随记

    xrdb -merge .Xresources才能使urxvt的配色显示正常 xpmroot 包含在fvwm包中

  9. JqGrid TreeView使用

    1.前端 <script src="@Url.Content("~/Scripts/jquery/jquery-1.9.0.min.js")" type= ...

  10. 内存映射mmap

    Table of Contents 1. 什么是mmap 2. 使用方法 2.1. mmap构造器的格式 2.2. 例子1 2.3. 例子2 3. 其它 4. 参考资料 什么是mmap 通常在Unix ...