为什么ASP.NET MVC使用会话状态? [英] Why would ASP.NET MVC use session state?

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

问题描述

建议由ASP.NET团队使用高速缓存,而不是会议上,我们停止使用从WebForm的模式工作在过去几年会话。因此,我们通常都在会议在web.config关闭

 <的sessionState模式=关/>

但是,现在当我与此设置会抛出类错误测试出一个ASP.NET MVC应用程序 SessionStateTempDataProvider MVC框架里面,它要求我打开会话状态,我没有和它的工作。查看源,它使用会话:

 在SessionStateTempDataProvider.cs //行20
字典<字符串对象> tempDataDictionary =
httpContext.Session [TempDataSessionStateKey]作为字典<字符串对象&gt ;;

那么,为什么他们在这里使用会话?我缺少什么?

=============================================== =========

修改对不起,并不意味着这个职位上会话与缓存辩论,而是在ASP.NET MVC的背景下,我只是想知道为什么会在这里使用。在此博客帖子也斯科特Watermasysk提到,关闭会话是一个很好的做法,所以我只是想知道我为什么要打开它使用MVC从这里开始。


解决方案

会话用于TempData的商店

。 TempData的是会话状态的高度有限的形式,这将仅持续直至从某个用户的下一个请求。 (修改在MVC 2+,它一直持续到它旁边读)的TempData的目的是为了存储数据,然后做一个重定向,并有存储的数据提供给行动你只是重定向。

使用会话的TempData的商店意味着它已经处理会议将为TempData的工作,任何分布式缓存系统。会话使用时直接TempData的将避免做有几个好处。其中之一是,你不必清理自己的会话;对自己TempData的将过期。

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

<sessionState mode="Off" />

But, now when I'm testing out a ASP.NET MVC application with this setting it throws an error in class SessionStateTempDataProvider inside the mvc framework, it asked me to turn on session state, I did and it worked. Looking at the source it uses session:

// 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?

========================================================

Edit Sorry 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 post also 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.

解决方案

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. (Edit In 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.

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.

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

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