当前上下文中不存在“会话" [英] 'Session' does not exist in the current context

查看:33
本文介绍了当前上下文中不存在“会话"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following code, that uses session but i have an error in the line :

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"];
    }
}

推荐答案

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

The issue is that your class does not inherit from Page. you need to Change

public class ShoppingCart

public class ShoppingCart : Page

它会起作用

这篇关于当前上下文中不存在“会话"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆