Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying read-only session in ASP.NET MVC

Is there any way to specify that a given Controller or Action uses Session state in a read-only manner? In "old" ASP.NET we used to do something like this:

<%@ Page EnableSessionState="ReadOnly" %>

Is there an ASP.NET MVC equivalent? I'm looking to allow my application to serve multiple requests from the same client simultaneously, and turning off session completely is -not- an option in my case.

like image 241
Mark Avatar asked Jul 23 '09 16:07

Mark


People also ask

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

Sessionless Controller We can disable the session for those controllers using the SessionStateAttribute (set session state Behavior to Disabled) and we can get a slight performance improvement for our application.

How is session maintained in ASP.NET MVC?

ASP.NET MVC provides three ways (TempData, ViewData and ViewBag) to manage session, apart from that we can use session variable, hidden fields and HTML controls for the same. But like session variable these elements cannot preserve values for all requests; value persistence varies depending the flow of request.

Are sessions enabled by default in MVC?

By default, Asp.Net MVC support session state.


2 Answers

In Asp.Net MVC3 there is now a SessionStateAttribute that you can decorate your Controller with to force all actions into Read-Write, Read-only, or No session mode.

http://msdn.microsoft.com/en-us/library/system.web.mvc.sessionstateattribute(v=VS.98).aspx

like image 124
Jeff Fritz Avatar answered Oct 13 '22 00:10

Jeff Fritz


A similar question was asked here: Disable Session state per-request in ASP.Net MVC

The accepted answer deals with creating a custom route handler that ultimately skips binding to the session. This doesn't exactly answer your question (which is how to declare multiple actions to use readonly session access), but it seemed relevant enough to mention.

like image 9
Seth Petry-Johnson Avatar answered Oct 13 '22 00:10

Seth Petry-Johnson