未将对象引用设置到对象的实例

未将对象引用设置到对象的实例

本文介绍了C#会议"未将对象引用设置到对象的实例&QUOT。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASHX文件:

I have an ASHX file:

对象引用未设置为一个对象的
实例

在该行:

HttpContext.Current.Session["loggedIn"] = true

这是我如何使用会话是否正确?

Is this how I use sessions properly?

推荐答案

我猜想,会话是这里的罪魁祸首;这里参考,你可能想尝试添加:IRequiresSessionState 来处理程序(代码隐藏的ASHX)。所以,你应该是这样的:

I would guess that Session is the culprit here; with reference here, you might want to try adding : IRequiresSessionState to your handler (the code-behind for the ashx). So you should have something like:

public class Handler1 : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
        context.Session["loggedIn"] = true;
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}



还请注意,它更容易去跟背景传入,但 HttpContext.Current 应该工作了。

Note also that it is easier to talk to the context passed in, but HttpContext.Current should work too.

这篇关于C#会议"未将对象引用设置到对象的实例&QUOT。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!