using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PPT_DAL;
namespace PPT_Web.tool
{
/// <summary>
/// Login 的摘要说明
/// </summary>
public class Login : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string lo_usm = context.Request.Params["lo_usm"];
string lo_pwd = context.Request.Params["lo_pwd"];
UserEntity userModel = PPT_BLL.UserManager.Login(lo_usm,lo_pwd);
if (userModel != null)
{
context.Session["UserId"] = userModel.USR_ID;
context.Response.Write("{'result':'1'}");
}
else
{
context.Response.Write("{'result':'0'}");
}
} public bool IsReusable
{
get
{
return false;
}
}
}
}

问题:

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

解决方法:

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

  最终实现代码如下:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PPT_DAL;
using System.Web.SessionState;
namespace PPT_Web.tool
{
/// <summary>
/// Login 的摘要说明
/// </summary>
public class Login : IHttpHandler, IRequiresSessionState
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string lo_usm = context.Request.Params["lo_usm"];
string lo_pwd = context.Request.Params["lo_pwd"];
UserEntity userModel = PPT_BLL.UserManager.Login(lo_usm,lo_pwd);
if (userModel != null)
{
context.Session["UserId"] = userModel.USR_ID;
context.Response.Write("{'result':'1'}");
}
else
{
context.Response.Write("{'result':'0'}");
}
} public bool IsReusable
{
get
{
return false;
}
}
}
}

解决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. Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值

    E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...

  2. iOS执行时工具-cycript

    cycript是大神saurik开发的一个很强大的工具,能够让开发人员在命令行下和应用交互,在执行时查看和改动应用.它确实能够帮助你破解一些应用,但我认为这个工具主要还是用来学习其它应用的设计(主要是 ...

  3. plupload上传插件在SpringMVC中的整合

    前言:近期在给学院的站点做一个加入附件的功能,首先到了某某邮箱看了一下.简单有用.可是是flash做的,无法拷贝.就仅仅好上网找插件了.经过筛选.最终找到plupload这款插件(其实有的编辑器自带加 ...

  4. C#实现ByteBuffer类 .

    在写网络程序的时候,经常需要往一个数组里面压数据或者取数据,而Java中再Java.nio中有个ByteBuffer能很方便的实现,Delphi中也有个Stream类有着同样的功能,这里我就模仿JAV ...

  5. Cassandra在CQL语言层面支持多种数据类型

    Cassandra在CQL语言层面支持多种数据类型. CQL类型 对应Java类型 描述 ascii String ascii字符串 bigint long 64位整数 blob ByteBuffer ...

  6. Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestParam, @PathVariable--转载

    原文地址: @RequestMapping is one of the most widely used Spring MVC annotation.org.springframework.web.b ...

  7. Collections.sort(List<T> Comparator) 自定义排序

    Collections.sort(basicinfoList, new Comparator<MlisBasicinfo>() { @Override public int compare ...

  8. 3.3html学习笔记之链接

    iframe 元素会创建包含另外一个文档的内联框架 <iframe src=""/> 跳转链接 <a href="#here" target= ...

  9. 1.5 理解Analyzers,Tokenizers,Filters--目录

    这部分介绍了solr如何分解和处理文本数据的,它包含一下主题: 1.5.1 Analyzers,Tokenizers,Filters概述:主要介绍Analyzers,Tokenizers,Filter ...

  10. C Struct Hack

    It's not clear if it's legal or portable, but it is rather popular. #include <stdlib.h> #inclu ...