16

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.)

M4N
  • 94,805
  • 45
  • 217
  • 260
  • This setting has no effect in an ASP.NET MVC application. What are you trying to achieve? – Darin Dimitrov Sep 02 '10 at 20:58
  • @Darin Dimitrov: updated question – M4N Sep 02 '10 at 21:09
  • I see. Next question: why do you need to disable or set read-only session state per controller/action? – Darin Dimitrov Sep 02 '10 at 21:11
  • Well if I know that my controller or action does not need any session state, then I'd like to turn it off (especially if this results in some performance improvement). – M4N Sep 02 '10 at 21:13
  • @M4N - you're right, read/write session state has the overhead of single-threading requests on a per-session basis. In other words a single user can only make one call to the web server at a time if the session is in read/write mode. – Joel Mueller Sep 02 '10 at 21:14
  • 1
    possible duplicate of [Disable Session state per-request in ASP.Net MVC](http://stackoverflow.com/questions/1464203/disable-session-state-per-request-in-asp-net-mvc) and [Enable / disable session state per controller / action method](http://stackoverflow.com/questions/2250940/enable-disable-session-state-per-controller-action-method) – Darin Dimitrov Sep 02 '10 at 21:17

2 Answers2

20

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

Community
  • 1
  • 1
SamStephens
  • 5,721
  • 6
  • 36
  • 44
5

It seems that with ASP.NET MVC 3 it will be possible to have session-less controllers, e.g. see ScottGu's blog post or this post by Keith Dahlby.

M4N
  • 94,805
  • 45
  • 217
  • 260