We help IT Professionals succeed at work.

Call Session from .cs file

RecipeDan
RecipeDan asked
on
I am trying to call a Session variable from a class file. However I am getting an error System.Web.HttpContext.Current is a property but used like a type

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Test.App_Code
{
    public class TestClass
    {
           if (System.Web.HttpContext.Current.Session["ID"] != null) {
                   //Do Something
                }
                else {
                    //Do Something Else
                }
    }
}

Open in new window

Comment
Watch Question

Developer
Top Expert 2015
Commented:
Hi,

You need to create a method in the class and check the Session as follows.

 public class TestClass
    {
        public static string CheckSession()
        {
            if (System.Web.HttpContext.Current.Session["ID"] != null)
            {
                //Do Something
            }
            else
            {
                //Do Something Else
            }
        }
    }

Open in new window

Author

Commented:
Thank you