HttpContext.Current.Session 请求图像时为 null。为什么?
我正在构建一个 ASP MVC 应用程序。
例如,当我请求到控制器的路由时:
http://myserver/sales/id/5
会话对象 HttpContext.Current.Session 工作正常。
但是,当我请求图像或脚本时,例如:
http://myserver/Scripts/jquery-1.4.1.js
会话对象 HttpContext.Current.Session
为 null
我如何区分这种情况与其他情况没有“真实”会话(例如首次登录)?
I'm building a ASP MVC application.
When I request a route to a controller, for example:
http://myserver/sales/id/5
the session object HttpContext.Current.Session
is working perfectly.
However, when I'm requesting an image or a script, for example:
http://myserver/Scripts/jquery-1.4.1.js
the session object HttpContext.Current.Session
is null
How I can distinguish this situation from other when there is no "real" session (for example first login)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么需要一个会话来检索静态文件?
除非您使用动态处理程序,否则会话绝对不需要检索此类内容。
我的观点是,会话对于检索静态内容没有任何有用的目的,因此它不可用。
Why would you need a session for retrieving a static file?
Unless you are using a dynamic handler, there is absolutely no need for session to retrieve such content.
My point is that session does not serve any useful purpose for retrieving static content, so it is not available.
@Oded是对的,这是asp.net中的优化。如果您需要会话可用,那么您将需要将请求发送到已指示需要会话的处理程序。如果您正在编写自己的处理程序,那么它只需要实现标记接口 IRequiresSessionState。
这种优化不仅适用于当前请求,而且您作为使用同一会话的多个请求发出的任何并行请求都会以串行方式处理。阅读我的博客文章有关此的更多信息。
@Oded is right, this is an optimization in asp.net. If you need the session to be available, then you will need the request to go to a handler that has indicated it needs the session. If you are writing your own handler, then it simply needs to implement the marker interface IRequiresSessionState.
This optimization isn't just for the current request, but any parallel requests that you are making as multiple requests that use the same session are handled in a serial fashion. Read my blog post for more info on this.