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

查看:924
本文介绍了为什么在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 ,根据控制器
  • 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的,这意味着我不能为任何一个抽象创建可重用的工具code

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对象请求,响应,会议进行了介绍。他们重新present抽象类和各地的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.

例如对会话对象有<一个href="http://msdn.microsoft.com/en-us/library/system.web.httpsessionstatebase.aspx"><$c$c>HttpSessionStateBase及其实施<一个href="http://msdn.microsoft.com/en-us/library/system.web.httpsessionstatewrapper.aspx"><$c$c>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天全站免登陆