在 ASP.Net MVC 中禁用每个请求的会话状态 [英] Disable Session state per-request in ASP.Net MVC
问题描述
我正在 ASP.Net MVC 中创建一个 ActionResult 来提供图像.启用会话状态后,IIS 一次只能处理来自同一用户的一个请求.(这不仅在 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.)
因此,在一个有多个图片回调到这个Action的页面上,一次只能处理一个图片请求.它是同步的.
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.
我希望这个图像操作是异步的——我希望每个执行多个图像请求而不需要前一个完成.(如果图像只是静态文件,IIS 会以这种方式提供它们.)
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.)
因此,我想仅针对对该 Action 的调用禁用 Session,或者指定某些请求没有 Session 状态.有人知道这是如何在MVC中完成的吗?谢谢!
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!
推荐答案
与其实现一个动作过滤器,不如实现一个 RouteHandler
?
Rather than implementing an action filter for this, why don't you implement a RouteHandler
?
这是交易 - IRouteHandler
有一个方法 - GetHttpHandler
.当您向控制器发出 ASP.Net MVC 请求时,默认情况下,路由引擎通过创建 MvcRouteHandler
的新实例来处理请求,该实例返回一个 MvcHandler
.MvcHandler
是 IHttpHandler
的一个实现,它标有(惊喜!)IRequiresSessionState
接口.这就是正常请求使用 Session 的原因.
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 of MvcRouteHandler
, which returns an MvcHandler
. MvcHandler
is an implementation of IHttpHandler
which is marked with the (surprise!) IRequiresSessionState
interface. This is why a normal request uses Session.
如果 您可以关注我的博客文章,了解如何实现自定义 RouteHandler
(而不是使用 MvcRouteHandler)来提供服务图像 - 您可以跳过返回带有会话标记的 IHttpHandler
.
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-tagged IHttpHandler
.
这应该可以使 IIS 免于对您施加同步.它也可能会更高效,因为它跳过了处理过滤器的 MVC 代码的所有层.
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.
这篇关于在 ASP.Net MVC 中禁用每个请求的会话状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!