Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable / disable session state per controller / action method

We are building an ASP.NET MVC application which will be deployed behind a hardware load balancer that supports, among other things, caching.

Our proposal is to manually define which URL patterns should be cached by the load balancer. This will be quite an easy process for us as we have 'catalogue' pages which are relatively static, then 'order' pages which are not.

Must avoid using session state on cached pages, as the entire response is cached by the load balancer - this includes any cookies that are sent.

Ideally there would be an attribute which can be applied to controllers or action methods which allows selective use of session state, but there doesn't appear to be one. I realise that an approach like this would result in lost sessions if the use leaves the 'session zone' - that's fine.

Other than re-implementing the entire ASP.NET MVC HTTP controller... any suggestions?

Thanks in advance.

like image 614
jamiecon Avatar asked Feb 12 '10 10:02

jamiecon


People also ask

How can you disable sessions of a particular controller in MVC?

For example, for any controller, we do not want to use a session state; at this time we can mark this controller with the SessionState attribute and the behavior is disabled.

What is SessionStateBehavior?

The SessionStateBehavior enumeration is used with the SetSessionStateBehavior method. You can call this method to indicate that session state support is needed to handle a request, and pass a SessionStateBehavior enumeration value to specify what type of session state behavior applies to the request.


2 Answers

This is now moved from Futures into MVC3. There's a ControllerSessionState attribute (apparently will be named SessionState for the final release of MVC3), which can be applied to a controller, something like this:

[SessionState(SessionStateBehavior.Disabled)]
public class MyController : Controller
{
 ...

(But in the RC version, you must use ControllerSessionState

like image 54
Will Dean Avatar answered Sep 22 '22 22:09

Will Dean


This is included in MVC 2 Futures. See http://blogs.msdn.com/rickandy/archive/2009/12/17/session-less-mvc-controller.aspx for more info.

like image 40
Levi Avatar answered Sep 21 '22 22:09

Levi