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

在ashx文件中,设置session直接用

HttpContext.Current.Session["UserCode"] = usercode
会报“未将对象引用设置到对象的实例”错误。搜索到很多资料说没有判断session对象是否存在,我是设置session对象,自然不是因为这个错误的。通过查找资料看到http://www.cnblogs.com/tonysuen/archive/2010/12/07/1899595.html   的博文,修改了代码,调用了接口,完成了登陆功能。

ashx文件中使用session需要using System.Web.SessionState;

根据需要调用IRequiresSessionState接口(读写)或者IReadOnlySessionState(只读)

using System;
using System.Collections.Generic;
using System.Web;
using DMS;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Web.SessionState;

// 调用IRequiresSessionState接口
public class login1 : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string usercode = context.Request["usercode"].ToString();
string type = context.Request["type"].ToString();
string fun = context.Request["fun"].ToString();
switch (fun)
{
case "checkUser":
string pwd = context.Request["pwd"].ToString();
checkUser(type, usercode, pwd, context);
break;
default:
break;
}
}

//验证用户信息(用户名和密码)
public void checkUser(string type, string usercode, string pwd, HttpContext context)
{
string res = "";
string sql = @"select count(1) from " + type + " t where t.usercode='" + usercode + "'
and t.userpwd ='" + pwd + "' ";
try
{
DataTable dt = sqlCon.getDt(sql);
string count = dt.Rows[0][0].ToString();
if (count == "1")
{
//如果有数据,则设置session
HttpContext.Current.Session["UserCode"] = usercode;
HttpContext.Current.Session["type"] = type;
HttpContext.Current.Session["UserPwd"] = pwd;
res = "ok";
}
else
{
res = "er";
}
}
catch (Exception e)
{
res = "checkUser__" + e.ToString();
}
finally
{
context.Response.Write(res);
}
}
}

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

  1. IIS报错 未将对象引用设置到对象的实例。

    在vs中运行正常的项目 ,发布到IIS总是提示 未将对象引用设置到对象的实例. 运行静态页面 html正常,只是打开.aspx页面的时候报错,在确保了数据库,配置,权限均正常的情况下. 错误原因:先安 ...

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

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

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

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

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

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

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

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

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

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

  7. WPF中未将对象引用设置到对象的实例

    前几天,我开始了WPF的基础学习,一上来我就遇到了一个令我头痛的问题,按照书上的例子我写了一段属于自己的代码,一个简单的色调器.满心期待的编译运行,就出现了未将对象引用设置到对象的实例.我在网上查阅了 ...

  8. 出现"未将对象引用设置到对象的实例“问题的总结

    今天做机房收费系统时,将DataGridView中的数据导入到Excel中,当运行到这一句代码”xlApp.Cells(rows + 2, j + 1) = DataGridView1(j, rows ...

  9. 报警提示 System.NullReferenceException:“未将对象引用设置到对象的实例。

    System.NullReferenceException:“未将对象引用设置到对象的实例.是就因为Session在记录到服务器时,没有添加  IRequiresSessionState 所以运行时回 ...

随机推荐

  1. 把Debian 设置中文环境

    要支持区域设置,首先要安装locales软件包: apt-get install locales 然后配置locales软件包: dpkg-reconfigure locales 在界面中钩选上“zh ...

  2. CodeForces-1221A-2048 Game-思维题

    You are playing a variation of game 2048. Initially you have a multiset ss of nn integers. Every int ...

  3. PAT_A1090#Highest Price in Supply Chain

    Source: PAT A1090 Highest Price in Supply Chain (25 分) Description: A supply chain is a network of r ...

  4. python 13 字符编码

    转自 http://www.cnblogs.com/BeginMan/p/3166363.html 一.字符编码中ASCII.Unicode和UTF-8的区别 点击阅读:http://www.cnbl ...

  5. Neo4j使用简单例子

    Neo4j Versions Most of the examples on this page are written with Neo4j 2.0 in mind, so they skip th ...

  6. D3.js比例尺 序数比例尺(v3版本)

    上一章介绍了阈值比例尺:https://www.cnblogs.com/littleSpill/p/10825038.html.到目前所有的定量比例尺已经介绍完了. 现在给大家介绍一下序数比例尺. 定 ...

  7. java 和 IntelliJ IDEA 的一些配置

    jdk 的下载与配置https://jingyan.baidu.com/article/ca41422fe3b7261eae99edc6.html intellij IDEA软件java项目No SD ...

  8. js 高级 原型与原型链

    * 所有函数都有一个特别的属性: * `prototype` : 显式原型属性* 所有实例对象都有一个特别的属性: * `__proto__` : 隐式原型属性 1.  每个函数都有一个prototy ...

  9. Java中的常量池

    JVM中有: Class文件常量池.运行时常量池.全局字符串常量池.基本类型包装类对象 常量池 Class文件常量池: class文件是一组以字节为单位的二进制数据流,在java代码的编译期间,编写的 ...

  10. python 九九乘法口诀

    for i in range(1,10):    for j in range(1,i+1):        print(j,"*",i,"=",i*j,&qu ...