Page.EnableSessionState equivalent in ASP.NET MVC

M4N picture M4N · Sep 2, 2010 · Viewed 7k times · Source

With ASP.NET WebForms it is possible to set the session state mode in the page directive:

<%@ Page EnableSessionState="true|false|ReadOnly" %>

Is the same configuration also possible in ASP.NET MVC (e.g. per controller or per action) and if so, how?

(In other words: can I disable or set to read-only session state per controllers/actions? I assume having the session state read/write will result in some overhead, so it might be useful if session state could be turned off if not required.)

Answer

SamStephens picture SamStephens · Jan 10, 2012

The ASP.NET MVC 3 equivalent to this appears to be the SessionState attribute, which you apply at the controller level - e.g.

[SessionState(SessionStateBehavior.ReadOnly)]

See http://msdn.microsoft.com/en-us/library/system.web.mvc.sessionstateattribute.aspx for more info.

Thanks to https://stackoverflow.com/a/4235006/372926