c# - System.NullReferenceException 检查 if != null 时

标签 c# null handler ashx argumentnullexception

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

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

我得到这个指向这一行的错误:

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

有什么问题?

最佳答案

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

您的处理程序是否实现了 IRequiresSessionState ?否则 session 可能不可用。

来自 MSDN :

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

关于c# - System.NullReferenceException 检查 if != null 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5774248/

相关文章:

linux - Mac 上的 mmap 可以返回 null 吗?

Java值总是为空?

tsql - 如何通过 SQL 中的空检查将值设置为 true 或 false?

android - runnable 中的 handler.postDelayed 在 Kotlin 中显示语法错误 - Android

c# - 为 SVN 修改提交时的文本文件,让我的程序知道它的提交号

c# - HttpWebRequest HTTP header

c# - 如何通过名称获取方法处理程序? (C#)

android - 停止运行

c# - EMGU CV 中未处理 AccessViolationException

c# - 使用变量作为值而不是引用