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 141519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:47:22+00:00 2026-05-11T07:47:22+00:00

I’m looking for thoughts on how should I use Session in an ASP.NET MVC

  • 0

I’m looking for thoughts on how should I use Session in an ASP.NET MVC application? Especially when using masterpages and tryin to just get the data to the masterpage without bypassing the controller. This question started off by me asking a lot of little questions, but then I managed to mould it into a solution which as yet I have not implemented but one which is somewhat workable. Would appreciate any feedback.


My proposed solution aka ‘what i am about to implement unless someone says stop!’

I have my model classes inheriting from ModelBase — which contains the information needed by the masterpage (there is only one view per page) for certain things it displays in the masthead or footer as well as configuration driven settings based upon who is logged in.

My best solution is as follows – shown here for a ‘products page’:

Assumption: I have at some point already stuck certain data in session – for instance perhaps a partnerId which came in through a gateway page, or a currentLoggedInUserEmail property or a fully blown object.

I have a ModelBase class from which every model – such as ProductModel inherits

I have a MySiteControllerBase class (inherits from Controller) – which is subclassed by ProductController.

In my action method in ProductController I create the model for the product view with 'new ProductModel()'. This model class itself knows nothing about session or how to populate ModelBase. It essentially doesn’t even know about ModelBase – it just inherits from it. My chained constructor does nothing (because I don’t want to pass it Session).

I override View(...) in MySiteControllerBase for all the overloads that take a model parameter. I check to see if that parameter is of type ModelBase and if it is I populate the properties such as partnerid and currentLoggedInuserEmail. Fortunately because I’m inside a class that inherits from Controller I have direct access to Session so i can pull them straight out of there.

This method means that the properties on ModelBase are automatically populated just by me doing 'return View(model)'. However there is an obvious issue if the model for ProductModel needs to access anything defined on ModelBase. It’s going to get null because it isn’t populated yet.

This issue can be solved by passing in Session to new ProductModel(session) which would in turn pass it up the constructor chain to new ModelBase(session). I really dont like that solution though becasue I like to think of a model as a pretty dumb data structure that shouldn’t know about any external data constructs at all. Another solution might be to just wing it, and if i ever find that ProductController needs to consume anything defined in ModelBase that I just create a method MySiteControllerBase.UpdateModelBase(productModel, session) to explicitly populate it inside ProductController. I hope thats clear!

Other questions that come to mind are :

  • What about unit testing? Is there any abstraction around Session state in MVC or should I build my own? I did a search in the sourcecode for ‘session’ and nothing came up!
  • How does session tracking work with /REST/FUL/URLS in MVC? Are there any issues ith cookies off that I need to know about?
  • Should I think of session differently from how I traditionally have?
  • 1 1 Answer
  • 3 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. 2026-05-11T07:47:23+00:00Added an answer on May 11, 2026 at 7:47 am

    Although there is nothing, in principle, wrong with using Session in ASP.NET MVC applications (well, at least nothing more wrong than using it in other ASP.NET applications…), I tend to feel it should be a last resort, when other things don’t work.

    Although your question is, generally, very well-written, you don’t go into any great detail on what you propose to store in Session. The two examples I found in your question are:

    • Current user e-mail
    • partnerid

    The user’s e-mail address is already available from forms authentication, if you are using that, and can be added to other ASP.NET membership providers which don’t already support it. It’s not clear what partnerid actually is, but I’m skeptical that the Session is the only possible place to store it.

    On the other hand, it’s entirely possible that you need to store stuff you haven’t told us about which would really only fit in the session.

    So before you go too far down this road, make sure that other solutions are not already available for the data you need to store.

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

Sidebar

Related Questions

No related questions found

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.