asp.net-mvc 为什么 ASP.NET MVC 会使用会话状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/387124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why would ASP.NET MVC use session state?
提问by Ray
Recommended by the ASP.NET team to use cache instead of session, we stopped using session from working with the WebForm model the last few years. So we normally have the session turned off in the web.config
由 ASP.NET 团队推荐使用缓存而不是会话,我们在过去几年停止使用会话来处理 WebForm 模型。所以我们通常在 web.config 中关闭会话
<sessionState mode="Off" />
But, now when I'm testing out a ASP.NET MVC application with this setting it throws an error in class SessionStateTempDataProviderinside the mvc framework, it asked me to turn on session state, I did and it worked. Looking at the source it uses session:
但是,现在当我使用此设置测试 ASP.NET MVC 应用程序时,它会SessionStateTempDataProvider在 mvc 框架内的类中引发错误,它要求我打开会话状态,我做到了并且它起作用了。查看它使用会话的源:
// line 20 in SessionStateTempDataProvider.cs
Dictionary<string, object> tempDataDictionary =
httpContext.Session[TempDataSessionStateKey] as Dictionary<string, object>;
So, why would they use session here? What am I missing?
那么,他们为什么要在这里使用 session 呢?我错过了什么?
========================================================
================================================== ======
EditSorry didn't mean for this post to debate on session vs. cache, but rather in the context of the ASP.NET MVC, I was just wondering why session is used here. In this blog postalso Scott Watermasysk mentioned that turning off session is a good practice, so I'm just wondering why I have to turn it on to use MVC from here on.
编辑抱歉,这篇文章并不是要讨论会话与缓存,而是在 ASP.NET MVC 的上下文中,我只是想知道为什么在这里使用会话。在这篇博文中,Scott Watermasysk 还提到关闭会话是一个很好的做法,所以我只是想知道为什么我必须从这里打开它才能使用 MVC。
采纳答案by Craig Stuntz
Session is used for the TempData store. TempData is a highly limited form of session state which will last only until the next request from a certain user. (EditIn MVC 2+, it lasts until it is next read.) The purpose of TempData is to store data, then do a redirect, and have the stored data be available to the action to which you just redirected.
Session 用于 TempData 存储。TempData 是一种高度受限的会话状态形式,它只会持续到来自某个用户的下一个请求。(在 MVC 2+ 中编辑,它一直持续到下一次读取。) TempData 的目的是存储数据,然后进行重定向,并使存储的数据可用于您刚刚重定向到的操作。
Using Session for the TempData store means that any distributed caching system which already handles Session will work for TempData. Avoiding using Session directly when TempData will do has a couple of advantages. One is that you don't have to clean up the Session yourself; TempData will "expire" on its own.
将 Session 用于 TempData 存储意味着任何已经处理 Session 的分布式缓存系统都可以用于 TempData。当 TempData 可以使用时避免直接使用 Session 有几个优点。一是你不必自己清理Session;TempData 将自行“过期”。
回答by Darin Dimitrov
Recommended by the ASP.NET team to use cache instead of session
ASP.NET 团队推荐使用缓存而不是会话
@ray247, could you provide a reference for this? Session and Cache are different by nature and should be used depending on application requirements. For example storing user specific data into the cache could lead to undesired behavior. Of course if you really want to avoid using session you could provide your own implementation of the ITempDataProviderinterface.
@ray247,你能提供一个参考吗?Session 和 Cache 本质上是不同的,应该根据应用需求来使用。例如,将用户特定数据存储到缓存中可能会导致不良行为。当然,如果您真的想避免使用 session,您可以提供自己的ITempDataProvider接口实现。
回答by maxnk
Hmm... May be you've read about persisting of the heavy objects or relatively rarely accessed objects - it's definitely better to put them into cache, but for light objects or for data that is required at every request there is no better technique than put them into Session.
嗯...也许你读过关于持久化重对象或相对很少访问的对象 - 将它们放入缓存肯定更好,但是对于轻对象或每个请求都需要的数据,没有比这更好的技术了将它们放入会话中。
Sessions are not evil if you are using them correctly.
如果您正确使用会话,会话并不是邪恶的。
回答by rajesh pillai
Just an additional thought. TempData has its own purpose and MS knew there will be different school of thoughts with respect to TempData persistent mechanism. So, by default they made the persistent store to be SessionState. But the design is still very flexible. Based on the needs of the project and the governance that guides it you can create your own tempdata provider to suit specific requirements.
只是一个额外的想法。TempData 有它自己的目的,MS 知道在 TempData 持久化机制方面会有不同的思想流派。因此,默认情况下,他们将持久存储设置为 SessionState。但是设计还是很灵活的。根据项目的需求和指导它的治理,您可以创建自己的临时数据提供程序以满足特定要求。
Here are some pointers to the resources TempData
这里有一些指向资源TempData 的指针
Here are some additional improvements in TempData implementation TempData Improvements
以下是 TempData 实现的一些额外改进 TempData Improvements
Here's an alternative implementation using MS Velocity Distributed Caching. Velocity TempData Provider
这是使用 MS Velocity 分布式缓存的替代实现。 速度临时数据提供程序

