How to integrate session state into an ASP.NET MVC application?

I'm looking for thoughts on how to use Session in an ASP.NET MVC application? Especially when using master pages and trying to simply get data to the main page without bypassing the controller. This question began with me, asking many small questions, but then I managed to turn it into a solution that has not yet been implemented, but one that is somewhat doable. Thank you for your feedback.


My proposed solution is β€œwhat am I going to implement if someone doesn't say stop!” "

I have model classes inheriting from ModelBase - which contain the information needed on the main page (there is only one view on the page) for certain things that it displays in the mast or footer, as well as settings based on the configuration based on that who logged in.

My best solution is this: here is the product page:

Presumably: at some point, I was already stuck certain data in the session - for example, perhaps partnerId , which entered through the gateway page, or the currentLoggedInUserEmail property or a completely blown object.

I have a ModelBase class from which each model (e.g. ProductModel inherits

I have a class MySiteControllerBase (inherited from Controller) that is subclassed by ProductController .

In my action method in ProductController I create a model to represent the product using 'new ProductModel()' . This model class itself does not know anything about the session or how to populate the ModelBase . In fact, he does not even know about ModelBase - he simply inherits from it. My chain constructor does nothing (because I don't want to pass it a Session ).

I override View(...) in MySiteControllerBase for all overloads that accept a model parameter. I check if this parameter is of type ModelBase , and if I populate properties like partnerId and currentLoggedInUserEmail . Fortunately, because I'm inside a class that inherits from Controller , I have direct access to Session , so I can get them straight from there.

This method means that the properties on ModelBase automatically populated only by me, doing a 'return View(model)' . However, there is an obvious problem if for a model for ProductModel you need to access something specific on ModelBase . It is going to get zero because it is not full yet.

This problem can be solved by passing to Session in the new ProductModel(session) , which, in turn, will pass the constructor chain to the new ModelBase(session) . I really don't like this solution, although I like to think of the model as a rather dumb data structure that should not be aware of any external data constructs at all. Another solution might just be his wing, and if I ever find that the ProductController should consume everything that is defined in ModelBase , I just create the MySiteControllerBase.UpdateModelBase(productModel, session) method to explicitly populate it inside the ProductController . Hope this is clear!

Other questions that come to mind are:

  • What about unit testing? Is there any abstraction around session state in MVC or should I create my own? I did a search in the source code for the session and nothing worked!
  • How does session tracking work with / REST / FUL / URLS in MVC? Are there any cookie issues that I need to know about?
  • Should I think of a session differently than I have traditionally had?
+7
asp.net-mvc session master-pages
source share
2 answers

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

Although your question is usually very well written, you do not delve into any detailed information about what you propose to keep in the session. Two examples that I found in your question:

  • Current user email
  • Partnerid

A custom email address is already accessible from forms authentication if you use it, and can be added to other ASP.NET membership providers that do not yet support it. It is not clear which partner actually exists, but I am skeptical that the session is the only possible place to store it.

On the other hand, it is entirely possible that you need to store things that you didn’t tell us about that really only work in a session.

So, before you go too far along this road, make sure that other data is not yet available for the data that needs to be saved.

+1
source share

As for unit testing, you'll need a fake HttpContext object (from HttpContextBase) and a fake session object (from SessionStateBase). Or you can do what we do and use the Phil Haacks HttpSimulator . Not an ideal solution, but there are so many closely related objects that join together when you do something with asp that you will never find anything particularly elegant. We found that we stumbled upon it so hard that it was worth it to grab these classes and paste them into a helper library.

Cookies work by domain, so there are actually no problems. You can always set up a session with or without cookies.

In general, be very stingy with what you keep in the session. But this also applies to Webforms.

+1
source share

All Articles