Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ';会议';在当前上下文中不存在_C#_Asp.net_Session - Fatal编程技术网

C# ';会议';在当前上下文中不存在

C# ';会议';在当前上下文中不存在,c#,asp.net,session,C#,Asp.net,Session,我有以下使用会话的代码,但行中有一个错误: if (Session["ShoppingCart"] == null) 错误为cS0103:当前上下文中不存在名称“Session”有什么问题 using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using Sy

我有以下使用会话的代码,但行中有一个错误:

if (Session["ShoppingCart"] == null)
错误为
cS0103:当前上下文中不存在名称“Session”
有什么问题

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"];
    }
}
使用系统;
使用系统数据;
使用系统配置;
使用System.Linq;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.HTMLControl;
使用System.Web.UI.WebControl;
使用System.Web.UI.WebControl.WebParts;
使用System.Xml.Linq;
使用System.Collections.Generic;
使用System.Web.SessionState;
/// 
///ShoppingCart的摘要说明
/// 
公共类购物车
{
名单;
公共购物车()
{
if(会话[“ShoppingCart”]==null)
列表=新列表();
其他的
列表=(列表)会话[“ShoppingCart”];
}
}

问题在于您的类没有从页面继承。你需要改变

public class ShoppingCart

而且它将工作

使用

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

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

您需要通过继承
页面
将类转换为
页面
,或者传入
会话
,或者使用
HttpContext.Current.Session
在我的情况下,只需尝试捕获块修复问题,如下所示:

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)
        {}
    }

如果您想直接使用会话,只需添加以下名称空间


使用system.web.mvc

哪种方法?你是说构造器吗?在另一个类中,您可以从非会话页面或线程调用它!!你有没有给会议或其他什么命名?我猜HttpContext.Current为null或HttpContext.Current.Session为null。。。但我不知道为什么在信息如此之少的情况下,这个HttpContext.Current.Session[“ShoppingCart”]很酷。谢谢:)但是我得到了这个异常System.NullReferenceException!!
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)
        {}
    }