gurehbgui
gurehbgui

Reputation: 14694

Access ASP.NET Session in the Background

I'm setting a Session in my Asp.NET Code like this:

public class MyController : Controller
{
    public ActionResult Index()
    {
        Session["AdminID"] = id;

        return View();
    }
}

But now I want to access this Session in a file which is not a Controller in the background of the project. Is this possible?

S.th. like this:

public class MyClass
{
    public int Foo()
    {
        return Session["AdminID"]
    }
}

Upvotes: 0

Views: 64

Answers (1)

volpav
volpav

Reputation: 5128

Use HttpContext.Current.Session:

using System.Web;

// ...

public int Foo()
{
  return (int)HttpContext.Current.Session["AdminID"];
}

Upvotes: 3

Related Questions