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!
Rather than implementing an action filter for this, why don't you implement a
RouteHandler
?Here's the deal -
IRouteHandler
has one method -GetHttpHandler
. When you make an ASP.Net MVC request to a controller, by default the routing engine handles the request by creating a new instance ofMvcRouteHandler
, which returns anMvcHandler
.MvcHandler
is an implementation ofIHttpHandler
which is marked with the (surprise!)IRequiresSessionState
interface. This is why a normal request uses Session.If you follow my blog post on how to implement a custom
RouteHandler
(instead of using MvcRouteHandler) for serving up images - you can skip returning a session-taggedIHttpHandler
.This should free IIS from imposing synchronicity on you. It would also likely be more performant because it's skipping all the layers of the MVC code dealing with filters.
Change DefaultCOntrollerFactory to custom ControllerFactory class. Default Controller.TempDataProvider use SessionStateTempDataProvider. you can change it.
1.Set web.config/system.web/sessionState:mode="Off".
2.create DictionaryTempDataProvider class.
3.Create DictionaryTempDataControllerFactory
4.In global.asax.cs Apprication_Start event set DictionaryTempDataControllerFactory.
On our server, IIS doesn't even know about sessions - it's the ASP.NET stack that handles one request per session at a time. Static files, like images, are never affected.
Is it possible that your ASP.NET app is serving the files instead of IIS?
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
See http://msdn.microsoft.com/en-us/library/system.web.mvc.sessionstateattribute.aspx for more info.
Thanks to https://stackoverflow.com/a/4235006/372926
I also came across the same problem and after doing R&D this link worked for me Reference: https://techatfingers.wordpress.com/2016/06/14/session-state-on-action/
1> Create custom Attribute
2. Override
3. Register class in Global.asax
SessionState attribute is quite helpful if u use mvc3. How to achieve this with mvc2 needs a little more coding.
Idea is to tell the asp.net that specific request wont use session object.
So, Create a custom route handler for specific requests
SessionStateBehavior enum has 4 members, you should use "disabled" or "readonly" modes to get async behavior.
After creating this custom route handler, be sure that your specific requests goes through this handler. This can be done via defining new routes at Global.asax
Adding this route makes all your requests to be handled by your custom route handler class. You can make it specific by defining different routes.