将整个模型类成员存储在会话中

Asp*_*Asp 6 c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

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

Session[int]/Session[string]

我需要检索

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

该课程中存在的任何内容.

Dav*_*ich 9

你可以存储

Session["UserProfile"]=UserProfile; // User Profile being an object
Run Code Online (Sandbox Code Playgroud)

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

UserProfile profile = (UserProfile) Session["UserProfile"]
Run Code Online (Sandbox Code Playgroud)