为什么 ASP.NET 中有两种不兼容的会话状态类型? [英] Why are there two incompatible session state types in ASP.NET?

查看:24
本文介绍了为什么 ASP.NET 中有两种不兼容的会话状态类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了两种在 ASP.NET MVC 中处理会话数据的方法:

I see two means of working with session data in ASP.NET MVC:

  • System.Web.SessionState.HttpSessionState,在 HttpApplication
  • 上可用
  • System.Web.HttpSessionStateBase,在 Controller
  • 上可用
  • System.Web.SessionState.HttpSessionState, available on HttpApplication
  • System.Web.HttpSessionStateBase, available on Controller

存储在一个中的数据似乎在另一个中可用.

Data stored in one seems to be available in the other.

不幸的是,这两种类型的唯一共同祖先是 System.Object,这意味着我无法为两者的抽象创建可重用的实用程序代码.

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.

为什么 API 是这样的?我缺少的两者之间是否存在重要区别?

Why is the API this way? Is there an important difference between the two that I am missing?

推荐答案

在 ASP.NET MVC 中引入了对经典 HttpContext 对象 Request、Response、Session 的抽象.它们代表抽象类,并在整个 MVC 框架中公开以隐藏底层上下文并简化单元测试,因为抽象类可以被模拟.

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.

例如,对于会话对象,您有 HttpSessionStateBase 及其实现 HttpSessionStateWrapper.

For example for the session object you have HttpSessionStateBase and its implementation HttpSessionStateWrapper.

以下是如何在经典 ASP.NET 会话和抽象之间转换的示例:

Here's an example of how to convert between the classic ASP.NET session and the abstraction:

HttpSessionStateBase session = new HttpSessionStateWrapper(HttpContext.Current.Session);

因此,您所指的 System.Web.SessionState.HttpSessionState 是自经典 ASP.NET 1.0 以来就存在的底层会话对象.在 MVC 中,这个对象被包装成一个 HttpSessionStateWrapper.但由于 ASP.NET MVC 是一个 ASP.NET 应用程序,您仍然会获得 Global.asax,您在其中拥有裸会话.

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 a HttpSessionStateWrapper. But since ASP.NET MVC is an ASP.NET application you still get the Global.asax in which you have the bare session.

这篇关于为什么 ASP.NET 中有两种不兼容的会话状态类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆