Session variables in ASP.Net

0

I have a method in AJAX that sends me a certain value, when so requested, for a session variable (a list of strings in this case).

Session Variable

private static List<string> ListData {
  get{return (List<string>) Session["ListDataSession"];}
  set {Session["ListDataSession"]=value;}

}

AJAX Method

 $.ajax({
        type: "POST",
        url: "Teste.aspx/UpdateData",
        data: '{name: "' + "newValue"+ '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function(response) {
        }
    });

C # method

[System.Web.Services.WebMethod]
public static bool UpdateData(string name)
{
    UpdateData.Add(name);
    return true;
}

How to make this single session variable in this type of cases where I need to have declared a static variable to manage information coming via AJAX?

This issue arises because opening two tabs where the same variable is used can not guarantee the integrity of the data.

    
asked by anonymous 27.04.2014 / 12:14

1 answer

1

I believe it would work that way

private List<string> ListData 
{
get {return (List<string>) HttpContext.Current.Session["ListDataSession"];}
set {HttpContext.Current.Session["ListDataSession"]=value;}

}

In your C # method

[System.Web.Services.WebMethod(EnableSession = true)]
public static bool UpdateData(string name)
{
  UpdateData.Add(name);
  return true;
}

I think this OS link answers your question. link

It is never good to use static properties in this case, because if two users are accessing the application at the same time they will access the same data.

    
27.04.2014 / 19:56