Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6003659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:09:10+00:00 2026-05-23T01:09:10+00:00

I created Get/Set HttpContext Session Methods in BaseController class and also Mocked HttpContextBase and

  • 0

I created Get/Set HttpContext Session Methods in BaseController class and also Mocked HttpContextBase and created Get/Set methods.

Which is the best way to use it.

    HomeController : BaseController
    {
        var value1 = GetDataFromSession("key1") 
        SetDataInSession("key2",(object)"key2Value");

        Or

        var value2 = SessionWrapper.GetFromSession("key3");
        GetFromSession.SetDataInSession("key4",(object)"key4Value");
    }

   public class BaseController : Controller
   {
       public  T GetDataFromSession<T>(string key)
       {
          return (T) HttpContext.Session[key];
       }

       public void SetDataInSession(string key, object value)
       {
          HttpContext.Session[key] = value;
       }
   }

Or

  public class BaseController : Controller
  {
     public ISessionWrapper SessionWrapper { get; set; }

     public BaseController()
     {
       SessionWrapper = new HttpContextSessionWrapper();
     }
  }

  public interface ISessionWrapper
  {
     T GetFromSession<T>(string key);
   void    SetInSession(string key, object value);
  }

  public class HttpContextSessionWrapper : ISessionWrapper
  {
     public  T GetFromSession<T>(string key)
     {
        return (T) HttpContext.Current.Session[key];
     }

     public void SetInSession(string key, object value)
     {
         HttpContext.Current.Session[key] = value;
     }
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:09:10+00:00Added an answer on May 23, 2026 at 1:09 am

    The second one seems the best. Although I would probably write those two as extension methods to the HttpSessionStateBase instead of putting them into a base controller. Like this:

    public static class SessionExtensions
    {
        public static T GetDataFromSession<T>(this HttpSessionStateBase session, string key)
        {
             return (T)session[key];
        }
    
        public static void SetDataInSession<T>(this HttpSessionStateBase session, string key, object value)
        {
             session[key] = value;
        }
    }
    

    and then inside the controllers, or helpers, or something that has an instance of HttpSessionStateBase use it:

    public ActionResult Index()
    {
        Session.SetDataInSession("key1", "value1");
        string value = Session.GetDataFromSession<string>("key1");
        ...
    }
    

    Writing session wrappers is useless in ASP.NET MVC as the HttpSessionStateBase provided by the framework is already an abstract class which could be easily mocked in unit tests.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a good way to get the process name that created a file
I've created a webpage which is inherited from another class something like : class
Can I get the images used in the self created theme direct or do
I've created a WCF service and when I browse to the endpoint I get
I need to get the version information of a DLL file I created in
Just trying to get up to speed with the SDK... So, I've created my
When running wsdl.exe on a WSDL I created, I get this error: Error: Unable
If I have a datetime field, how do I get just records created later
I set about designing my own class to hold a series of radio buttons.
I have to created a dynamic URLcontaining the user id and email parameters, which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.