解决ashx文件下的Session“未将对象引用设置到对象的实例”
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“未将对象引用设置到对象的实例”的更多相关文章
- 2014-08-26 解决HttpContext.Current.Session在ashx文件中出现“未将对象引用设置到对象的实例”的问题
今天是在吾索实习的第35天. 最近在使用HttpContext.Current.Session来获取Session["..."]的值时,常常会弹出错误——“未将对象引用设置到对象的 ...
- ashx文件中使用session提示“未将对象引用设置到对象的实例”
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data;u ...
- C#一般处理程序设置和读取session(session报错“未将对象引用设置到对象的实例”解决)
登陆模块时,用到了session和cookie.在一般处理程序中处理session,一直报错.最后找到问题原因是需要调用 irequiressessionstate接口. 在ashx文件中,设置ses ...
- 解决:getWeatherbyCityName(city),服务器无法处理请求。 ---> 未将对象引用设置到对象的实例。
原文:getWeatherbyCityName(city),服务器无法处理请求. ---> 未将对象引用设置到对象的实例. 解决方法:不要直接使用 “服务引用” , 添加为 “Web 引用” 最 ...
- 解决使用DevExpress开发错误:未将对象引用设置到对象的实例
在使用DevExpress是总是会出现一些状况.这次同事在他的机器上调试完成的代码发过来,却出现"未将对象引用设置到对象的实例"的错误,提示是Resources.resx的问题.另 ...
- asp.net 中Session的运用,及抛出错误“未将对象引用设置到对象的实例”
1. 页面载入后,必须要等到page_Load方法执行建立 page对象后才可以使用Session 2. 在.aspx和.cs文件中使用Session的区别 (1).aspx: Session[&qu ...
- 一般处理程序中使用Session出现未将对象引用设置到对象的实例
遇到问题:未将对象引用设置到对象的实例 那就在你的一般处理程序中加入红色背景的代码吧 using System; using System.Collections.Generic; using Sys ...
- HttpContext.Current.Session[strName]未将对象引用设置到对象的实例
项目开发是在4.5.1上,不知道为啥客户提供的服务器上安装的是4.5,差别不大也没去升级,然后部署MVC的时候web.config报错 <system.web> <compilati ...
- SQL Sever无法打开链接对话框,未将对象引用设置到对象的实例。(AppIDPackage)
前几天刚做完系统,先装的是SQL Sever2008,装完后还试了一下,OK~没问题,然后就继续装VS2012等一些软件.搞到很晚没有继续试试就睡了,第二天运行SSMS出问题了..(如图 1.0 所示 ...
随机推荐
- c++课程实训 银行储蓄系统
基本要求:定义了用户类(User)和银行类(Bank),用成员函数实现各种功能,多文件组织程序.能用文本文件存取数据(如演示样例中给出的技术): 拓展方向: 序号 加分项目 细 则 1 改 ...
- Jquery 右键菜单(ContextMenu)插件使用记录
目前做的项目需要在页面里面用右键菜单,在网上找到两种jquery的右键菜单插件,但是都有各种问题.所以就自己动手把两种插件结合了下. 修改后的右键菜单插架可以根据绑定的触发页面元素不同,复用同一个菜单 ...
- 如何在XAF中显示自定义窗体和控件
https://www.devexpress.com/Support/Center/Example/Details/E911
- asp.net 获取url
string url = Request.Url.ToString(); this.ImageLogo.ImageUrl = "http://" + Request.Url.Aut ...
- android获取/更改gps和WIFI状态
一.WIFI状态的获取和更改 适用于 SDK1.0 , SDK1.5 1.获取WIFI状态 方法1:通过WifiManager进行操作 1WifiManager wifiManager = (Wifi ...
- oc-14-对象方法调用类方法
Person.h #import <Foundation/Foundation.h> @interface Person : NSObject { @public int _age; fl ...
- 基于jQuery带图标的多级下拉菜单
之前为大家分享了很多导航菜单.今天我们要来分享一款很不错的jQuery左侧带小图标的多级下拉菜单,菜单是垂直的,每一个菜单项带有一个小图标,看起来非常专业.并且菜单支持无限极下拉,所以对各位Web开发 ...
- POJ1651:Multiplication Puzzle(区间DP)
Description The multiplication puzzle is played with a row of cards, each containing a single positi ...
- Long Long Message 后缀数组入门题
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 22564 Accepted: 92 ...
- 1.4.8 拼凑在一起(putting the pieces together)
putting the pieces together 在最高的级别,schema.xml结构如下, <schema> <types> <fields> <u ...