Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Session state per-request in ASP.Net MVC

I am creating an ActionResult in ASP.Net MVC to serve images. With Session state enabled, IIS will only handle one request at a time from the same user. (This is true not just in MVC.)

Therefore, on a page with multiple images calling back to this Action, only one image request can be handled at a time. It's synchronous.

I'd like this image Action to be asynchronous -- I'd like multiple image requests to each execute without needing the previous one to complete. (If the images were just static files, IIS would serve them up this way.)

So, I'd like to disable Session just for calls to that Action, or to specify that certain requests do not have Session state. Anyone know how this is done in MVC? Thanks!

like image 615
Matt Sherman Avatar asked Sep 23 '09 06:09

Matt Sherman


People also ask

Are sessions enabled by default in ASP.NET MVC?

By default, Asp.Net MVC support session state. Session is used to store data values across requests. Whether you store some data values with in the session or not Asp.Net MVC must manage the session state for all the controllers in your application that is time consuming.

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.


1 Answers

If anyone is in the situation I was in, where your image controller actually needs read only access to the session, you can put the SessionState attribute on your controller

[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

like image 193
SamStephens Avatar answered Sep 22 '22 02:09

SamStephens