Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the value of a session variable inside a static method?

I am using ASP.NET page methods with jQuery.... How do I get the value of a session variable inside a static method in C#?

protected void Page_Load(object sender, EventArgs e)
{
    Session["UserName"] = "Pandiya";
}

[WebMethod]
public static string GetName()
{
    string s = Session["UserName"].ToString();
    return s;
}

When I compile this I get the error:

An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Session.get'`

like image 716
ACP Avatar asked Apr 05 '10 06:04

ACP


People also ask

How do you access a variable in a static method?

Static variables in Java belong to the class i.e it is initialized only once at the start of the execution. By using static variables a single copy is shared among all the instances of the class, and they can be accessed directly by class name and don't require any instance.

How can we use session in static method?

Just use the below syntax: HttpContext. Current. Session["SessionName"].

Can static methods return values?

A static method can also provide an array as a return value.


4 Answers

HttpContext.Current.Session["..."]

HttpContext.Current gets you the current ... well, Http Context; from which you can access: Session, Request, Response etc

like image 192
jwwishart Avatar answered Oct 05 '22 16:10

jwwishart


If you haven't changed thread, you can use HttpContext.Current.Session, as indicated by jwwishart.

HttpContext.Current returns the context associated with the thread. Obviously this means you can't use it if you've started a new thread, for example. You may also need to consider thread agility - ASP.NET requests don't always execute on the same thread for the whole of the request. I believe that the context is propagated appropriately, but it's something to bear in mind.

like image 28
Jon Skeet Avatar answered Oct 05 '22 16:10

Jon Skeet


Try this:

HttpContext.Current.Session["UserName"].ToString();
like image 38
Muhammad Awais Avatar answered Oct 05 '22 15:10

Muhammad Awais


You can access the current Session via HttpContext.Current - a static property through which you can retrieve the HttpContext instance that applies to the current web request. This is a common pattern in static App Code and static page methods.

string s = (string)HttpContext.Current.Session["UserName"];

The same technique is used to access the Session from within ASMX web methods decorated with [WebMethod(EnableSession = true)] because whilst such methods are not static they do not inherit from Page and thus do not have direct access to a Session property.

Static code can access the Application Cache in the same way:

string var1 = (string)HttpContext.Current.Cache["Var1"];

If the static code is inside another project we need to reference System.Web.dll. However, in this case it is generally best to avoid such a dependency because if the code is called from outside of an ASP.NET context HttpContext.Current will be null, for obvious reasons. Instead, we can require a HttpSessionState as an argument (we'll still need the reference to System.Web of course):

public static class SomeLibraryClass
{
    public static string SomeLibraryFunction(HttpSessionState session)
    {
       ...
    }
}

Call:

[WebMethod]
public static string GetName()
{
    return SomeLibraryClass.SomeLibraryFunction(HttpContext.Current.Session);
}
like image 29
Stephen Kennedy Avatar answered Oct 05 '22 17:10

Stephen Kennedy