System.NullReferenceException当检查!= null时 [英] System.NullReferenceException When checking if != null

查看:107
本文介绍了System.NullReferenceException当检查!= null时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASHX处理程序,我希望处理程序检查Session!= null.

I'm using an ASHX handler, i want the handler to check if Session != null.

if (context.Session["Username"] != null)

我在指向此行时遇到此错误:

And i get this error pointing this line:

System.NullReferenceException:对象引用未设置为对象的实例.

System.NullReferenceException: Object reference not set to an instance of an object.

出什么问题了?

推荐答案

if (context.Session["Username"] != null)

您的处理程序是否实现 IRequiresSessionState ?否则,会话可能不可用.

Does your handler implement IRequiresSessionState? Otherwise Session might not be available.

来自 MSDN :

指定目标HTTP处理程序 需要对以下内容的读写权限 会话状态值.这是一个标记 界面,没有任何方法.

Specifies that the target HTTP handler requires read and write access to session-state values. This is a marker interface and has no methods.

这篇关于System.NullReferenceException当检查!= null时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆