如何从会话中检索通用列表? [英] How to retrieve a generic list from session?

查看:57
本文介绍了如何从会话中检索通用列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个会话列表:

Session.Add("SessionList", mylist);

如何从会话中找回它?

推荐答案

var list = Session["SessionList"] as List<whatevertypeYouUsed>;

if (list != null){
   // blah...
}

我更喜欢使用 as 关键字,因为不能100%保证Session将包含该列表(由于应用程序池刷新,网站正在重启等).为您提供额外的防御,以避免 NullReferenceException .

I prefer to use the as keyword since there is no 100% guarantee that the Session will contain the list (due to application pool refresh, website being restarted, etc). Gives you that extra bit of defence to avoid a NullReferenceException.

这篇关于如何从会话中检索通用列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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