首页 > Web开发 > 详细

Asp.net在ashx文件中处理Session问题解决方法

时间:2015-11-27 21:27:04      阅读:419      评论:0      收藏:0      [点我收藏+]

今天做一个vb.net的登录实例,前台使用Ext中Ajax ,后台用ashx文件处理登录过程,并将用户储存在Session中总是出现在这个错误.  [NullReferenceException: 未将对象引用设置到对象的实例。]

从网上查了一下有以下几种原因:

1、ViewState 对象为Unll。

2、DateSet 空。

3、sql语句或Datebase的原因导致DataReader空。

4、声明字符串变量时未赋空值就应用变量。

5、未用new初始化对象。

6、Session对象为空。

开始以为是 Session对象为空的原因:将代码修改以下 

if context.Session.isNewSession then

     context.Session("UserName")=username ‘username为接受的用户名

.................

这样修改了以后,晕!!!!!,还是相同的错误.再Google吧

输入:ashx 处理 session   中于搜到了,太不容易,看来搜索也需要很多技巧

原来:在 ashx 中要使用Session ,需要继承System.Web.SessionState.IRequiresSessionState接口.默认的之继承了IHttpHandler接口

OK    直接看源代码吧

复制代码 代码如下:

<%@ WebHandler Language="VB" Class="logout" %>

Imports System

Imports System.Web

Imports System.Web.SessionState

Public Class logout : Implements IHttpHandler, IRequiresSessionState

  

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        context.Session.Clear()

        context.Response.Write("{‘success‘:‘true‘}") ‘JSON格式数据

    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable

        Get

            Return False

        End Get

    End Property

End Class

Asp.net在ashx文件中处理Session问题解决方法

原文:http://www.jb51.net/article/37351.htm

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!