为什么 ASP.NET 中有两种不兼容的会话状态类型?
我在 ASP.NET MVC 中看到了两种处理会话数据的方法:
System.Web.SessionState.HttpSessionState
,可在HttpApplication
System.Web.HttpSessionStateBase
上使用code>,在Controller
上可用 存储
在一个控制器中的数据似乎在另一个控制器中可用。
不幸的是,这两种类型的唯一共同祖先是 System.Object,这意味着我无法为这两种类型的抽象创建可重用的实用程序代码。
为什么 API 是这样的?我所缺少的两者之间有重要的区别吗?
I see two means of working with session data in ASP.NET MVC:
System.Web.SessionState.HttpSessionState
, available onHttpApplication
System.Web.HttpSessionStateBase
, available onController
Data stored in one seems to be available in the other.
Unfortunately the only common ancestor of these two types is System.Object
, meaning that I can't create reusable utility code for the abstraction of either.
Why is the API this way? Is there an important difference between the two that I am missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 ASP.NET MVC 中,引入了对经典 HttpContext 对象 Request、Response、Session 的抽象。它们代表抽象类,并在整个 MVC 框架中公开,以隐藏底层上下文并简化单元测试,因为抽象类可以被模拟。
例如,对于会话对象,您有
HttpSessionStateBase
及其实现HttpSessionStateWrapper
。下面是如何在经典 ASP.NET 会话和抽象之间进行转换的示例:
因此,您所指的 System.Web.SessionState.HttpSessionState 是自经典 ASP 以来就存在的基础会话对象.NET 1.0。在 MVC 中,该对象被包装到 HttpSessionStateWrapper 中。但由于 ASP.NET MVC 是一个 ASP.NET 应用程序,您仍然可以获得 Global.asax,其中包含裸会话。
In ASP.NET MVC abstractions over the classic HttpContext objects Request, Response, Session were introduced. They represent abstract classes and are exposed all over the MVC framework to hide the underlying context and simplify the unit testing because abstract classes can be mocked.
For example for the session object you have
HttpSessionStateBase
and its implementationHttpSessionStateWrapper
.Here's an example of how to convert between the classic ASP.NET session and the abstraction:
So the
System.Web.SessionState.HttpSessionState
which you are referring to is the underlying session object which existed ever since classic ASP.NET 1.0. In MVC this object is wrapped into aHttpSessionStateWrapper
. But since ASP.NET MVC is an ASP.NET application you still get the Global.asax in which you have the bare session.