How to access session variables from any class in ASP NET

0 votes

I have created a class file in the App_Code folder in my application. I have a session variable

Session["loginId"]

I want to access this session variables in my class, but when I am writing the following line then it gives error

Session["loginId"]

Can anyone tell me how to access session variables within a class which is created in app_code folder in ASP.NET?

Jul 23, 2020 in Angular by kartik
• 37,510 points
573 views

2 answers to this question.

0 votes

Hello @kartik,

Access the Session via the threads HttpContext:-

HttpContext.Current.Session["loginId"]

Hope it helps!!
Thank you!!

answered Jul 23, 2020 by Niroj
• 82,720 points
0 votes
Accessing session Variable in a class file requires you to make use of the HttpContext because are you working outside the asp.net page.  So in order for you to have access to page methods, you need the HttpContext.Current.

So your solution is

HttpContext.Current.Session["sessionName"];
answered Sep 3, 2020 by Okugbe
• 280 points

Related Questions In Angular

0 votes
1 answer

How can I strip HTML tags from a string in ASP.NET?

Hello @Kartik, If it is just stripping all HTML tags ...READ MORE

answered Jul 23, 2020 in Angular by Niroj
• 82,720 points
255 views
0 votes
1 answer

How to set session timeout in web.config?

Hello @kartik, If you want to set the ...READ MORE

answered Jul 23, 2020 in Angular by Niroj
• 82,720 points
2,127 views
0 votes
1 answer

How to create a URL in the controller .NET MVC?

Hello @kartik, If you just want to get ...READ MORE

answered Jul 23, 2020 in Angular by Niroj
• 82,720 points
644 views
0 votes
1 answer

How to allow download of .json file with ASP.NET?

Hello @kartik, If you want to manually add ...READ MORE

answered Jul 23, 2020 in Angular by Niroj
• 82,720 points
480 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,720 points
7,265 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,720 points
823 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,720 points
646 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,720 points
7,672 views
0 votes
1 answer

How to add “active” class to Html.ActionLink in ASP.NET MVC?

Hello @kartik, The way you handle your UI ...READ MORE

answered Jul 23, 2020 in Angular by Niroj
• 82,720 points
1,310 views
0 votes
1 answer

How do you bind an Enum to a DropDownList control in ASP.NET?

Hello @kartik, I probably wouldn't bind the data as it's ...READ MORE

answered Jul 23, 2020 in Angular by Niroj
• 82,720 points
800 views