【问题标题】:'Session' does not exist in the current context当前上下文中不存在“会话”
【发布时间】:2011-10-21 14:59:39
【问题描述】:

我有以下代码,它使用会话但我在行中有错误:

if (Session["ShoppingCart"] == null)

错误是cS0103: The name 'Session' does not exist in the current context有什么问题?

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Collections.Generic;
using System.Web.SessionState;
/// <summary>
/// Summary description for ShoppingCart
/// </summary>
public class ShoppingCart
{
    List<CartItem> list;
    public ShoppingCart()
    {
        if (Session["ShoppingCart"] == null)
            list = new List<CartItem>();
        else
            list = (List<CartItem>)Session["ShoppingCart"];
    }
}

【问题讨论】:

  • 哪种方法?你的意思是构造函数吗?在另一个班级
  • 您从非会话页面或线程中调用它。

标签: c# asp.net session


【解决方案1】:

问题是您的类没有从 Page 继承。你需要改变

public class ShoppingCart

public class ShoppingCart : Page

它会起作用的

【讨论】:

    【解决方案2】:

    使用

    if (HttpContext.Current == null || 
        HttpContext.Current.Session == null || 
        HttpContext.Current.Session["ShoppingCart"] == null)
    

    而不是

    if (Session["ShoppingCart"] == null)
    

    【讨论】:

    • 但是我得到了这个异常 System.NullReferenceException .. !!
    • 你有没有放弃会议或其他什么?我的猜测是 HttpContext.Current 为空或 HttpContext.Current.Session 为空......但我不知道为什么信息这么少
    • 这个 HttpContext.Current.Session["ShoppingCart"] 很酷。谢谢:)
    【解决方案3】:

    您需要通过从Page 继承将您的类转换为Page,或者传入Session,或者使用HttpContext.Current.Session

    【讨论】:

    • 但是我得到了这个异常 System.NullReferenceException .. !!
    【解决方案4】:

    就我而言,只有 try-catch 块修复问题,如下所示:

    protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            /// Using from Try-Catch to handle "Session state is not available in this context." error.
            try
            {
                //must incorporate error handling because this applies to a much wider range of pages 
                //let the system do the fallback to invariant 
                if (Session["_culture"] != null)
                {
                    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(Session["_culture"].ToString());
                    //it's safer to make sure you are feeding it a specific culture and avoid exceptions 
                    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Session["_culture"].ToString());
                }
            }
            catch (Exception ex)
            {}
        }
    

    【讨论】:

      【解决方案5】:

      如果你想直接使用会话,那么只需添加以下命名空间

      using system.web.mvc

      【讨论】:

        猜你喜欢
        • 2011-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-13
        • 1970-01-01
        • 2023-03-30
        • 2019-08-05
        相关资源
        最近更新 更多