简体   繁体   English

会话在asp.net中返回空对象

[英]session returns null object in asp.net

I am developing asp.net web application in which i am using Session to store user related data.Though i am storing data in session object when i am trying to retrieve it gives me error : 我正在开发asp.net Web应用程序,其中我正在使用Session来存储与用户相关的数据。虽然我在尝试检索数据时将数据存储在session对象中却给了我错误:

Object reference not set to an instance of an object.

Setting session variable 设置会话变量

somewhere in code behind of aspx page (myfile.aspx.cs) aspx页面(myfile.aspx.cs)后面代码中的某处

HttpContext.Current.Session["ProjectID"] = Request.QueryString["Pid"].ToString();

Retrieving Session Variable 检索会话变量

Normal Business Logic C# Class (Someclass.cs) 常规业务逻辑C#类(Someclass.cs)

sProjectID = HttpContext.Current.Session["ProjectID"].ToString();

It gives me error on above line. 它在上面的行给我错误。 It looks weird to me. 在我看来很奇怪。 can anyone explain this ? 有人可以解释吗?

Edit: 编辑:

Call to class pass through generic handler (.ashx) page 通过通用处理程序(.ashx)页面调用类

If it is really the line of code you showed, that produces exception, it means your "Normal Business Logic" is running in a non-HttpRequest handling thread, where thing such as Context and Session are not available. 如果确实是您所显示的代码行产生了异常,则表示您的“常规业务逻辑”正在非HttpRequest处理线程中运行,其中诸如Context和Session之类的东西不可用。 Check that HttpContext.Current and HttpContext.Current.Session are not null. 检查HttpContext.CurrentHttpContext.Current.Session是否不为null。

Update: 更新:

After seeing your edit - to have access to Session, your handler must include IRequiresSessionState in its class declaration. 看到您的编辑后-要访问Session,您的处理程序必须在其类声明中包括IRequiresSessionState See: HttpContext.Current.Session is null in Ashx file . 请参阅: Ashx文件中HttpContext.Current.Session为null

Can you reach in the debug mode line where you setting session values? 您能否到达设置会话值的调试模式行? If you can try to hit line after and inspect value in session by the key "ProjectID". 如果您可以尝试通过脚本“ ProjectID”在行中点击并检查会话中的值。

Try setting the cookie this way: 尝试通过以下方式设置Cookie:

HttpCookie cookie = HttpContext.Current.Request.QueryString["Pid"].ToString();
cookie.Expires = DateTime.Now.AddDays(7); // Set the expiration duration
HttpContext.Current.Response.Cookies.Set(cookie); // Set it to the response

And try getting the cookie this way on the next request: 并尝试以这种方式在下一个请求中获取Cookie:

string value = HttpContext.Current.Request.Cookies["Pid"] != null ? this.Page.Request.Cookies["Pid"].ToString() : null; // load it from request

Honestly, you should not try to receive your cookie in your business logic at all, as it has nothing to do with your website. 老实说,您完全不应该尝试从业务逻辑中接收cookie,因为它与您的网站无关。 Instead you should load the cookie in the UI (web) and pass the value as parameter to any function or property of your Someclass.cs in your business logic. 相反,您应该将Cookie加载到UI(网络)中,并将该值作为参数传递给业务逻辑中Someclass.cs的任何函数或属性。

Take a look at the N-tier model and it's reasons to do so. 看一下N层模型,这是这样做的原因。

You can't find any value in the session in the Debug mode.. Assign it to a reference variable like string and find whether it is returning a value.. 在调试模式下,您无法在会话中找到任何值。将其分配给字符串之类的引用变量,并确定其是否返回值。

and for a good practice check the session variable is null or not before assigning to any object.. 并为良好作法,在分配给任何对象之前,请检查会话变量是否为null。

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

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