6

在一个新创建的 MVC4 应用程序中,我想将所有 USERProfile 成员存储在一个会话中,这样我就可以在用户登录后访问所有值。但是 Session 数组对象只提出了 2 个替代方案

Session[int]/Session[string]

我需要找回

Session['username']; Session['age']; etc

该类中存在的任何内容。

4

2 回答 2

9

你可以存储

Session["UserProfile"]=UserProfile; // User Profile being an object

需要注意的是,当您检索您的个人资料时,您必须将其投射:

UserProfile profile = (UserProfile) Session["UserProfile"]
于 2013-02-18T20:54:30.473 回答
0

您列出的是可用于在会话中访问某些内容的密钥。键可以是 int 或字符串。

简单地做

Session.Add("MyProfile", USERProfileObject);

您可以将整个对象存储在会话中。

请注意:http: //msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.add.aspx

表明它需要一个字符串和一个对象。

于 2013-02-18T20:54:57.567 回答