简体   繁体   English

c#sessions“对象引用未设置为对象的实例。”

[英]c# sessions “Object reference not set to an instance of an object.”

I have an ASHX file: 我有一个ASHX文件:

Object reference not set to an instance of an object. 你调用的对象是空的。

On the line: 在线上:

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

Is this how I use sessions properly? 这是我如何正确使用会话?

I would guess that Session is the culprit here; 我猜这Session是罪魁祸首; with reference here , you might want to try adding : IRequiresSessionState to your handler (the code-behind for the ashx). 这里参考,你可能想尝试添加: IRequiresSessionState到你的处理程序(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;
        }
    }
}

Note also that it is easier to talk to the context passed in, but HttpContext.Current should work too. 还要注意,与传入的context交谈更容易,但HttpContext.Current应该工作。

ASHX handlers don't have session information by default. 默认情况下,ASHX处理程序没有会话信息。

See this page http://www.hanselman.com/blog/GettingSessionStateInHttpHandlersASHXFiles.aspx 请参阅此页http://www.hanselman.com/blog/GettingSessionStateInHttpHandlersASHXFiles.aspx

IRequiresSessionState 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM