2011-08-06 23 views
7

Mam następujący kod, który używa sesji, ale mam błąd w linii:„sesja” nie istnieje w bieżącym kontekście

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

błąd jest cS0103: The name 'Session' does not exist in the current context czym polega problem?

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

która metoda? masz na myśli konstruktora? w innej klasie – Adham

+1

Wywołujesz go ze strony innej niż sesja - lub z wątku. – Aristos

Odpowiedz

15

Problem polega na tym, że twoja klasa nie dziedziczy po Stronie.
Zmień public class ShoppingCart

do klasy publicznej shoppingcart: Strona

i będzie działać

+4

Dziedziczenie strony, gdy jej strona nie wydaje się być właściwym rozwiązaniem ... – Peter

34

Korzystając

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

zamiast

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

Ale otrzymałem ten wyjątek System.NullReferenceException .. !! – Adham

+0

Czy odłożyłeś sesję lub coś? Domyślam się, że HttpContext.Current ma wartość null lub HttpContext.Current.Session ma wartość null ... ale nie mam pojęcia, dlaczego z tak małą informacją – Peter

+0

ten HttpContext.Current.Session ["ShoppingCart"] był fajny. dzięki :) – Umitk

7

albo trzeba przekonwertuj swoją klasę na Page, dziedzicząc po Page lub przekazując Session lub użyj HttpContext.Current.Session.

+2

Ale dostaję ten wyjątek System.NullReferenceException .. !! – Adham

+0

Dziedziczenie strony, gdy jej strona nie jest stroną, nie wydaje się właściwym rozwiązaniem ... – Peter

0

Salam.

W moim przypadku tylko try-catch problemu blok poprawek, tak:

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

Tworzenie nowej strony i wklejając wszystko na nim, ze złamanym stronie nie działa. Praca polegała na zamknięciu Visual Studio i ponownym otwarciu. Złamany strona działało, potem

0

Jeśli chcesz korzystać bezpośrednio sesji następnie po prostu dodać następujących nazw

using system.web.mvc

Powiązane problemy