如何在MVC3剃须刀视图中使用会话变量,以保持数据? [英] How to Persist data using session variable in mvc3 razor view?

查看:105
本文介绍了如何在MVC3剃须刀视图中使用会话变量,以保持数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用剃刀MVC3应用工作。在验证用户后,我的帐户控制器,我得到来自数据库的用户客户端ID。在这里我要坚持的ClientID在Session变量。它使用跨所有控制器和剃刀观点。

I am working in MVC3 Application with Razor. In my Account controller after validating the user, i am getting the user ClientID from Database. Here i want to persist ClientID in Session variable. which was using across the all controller and Razor view.

我不知道,什么是实现this.OR如何在会话变量持久化数据的最佳方式。以及如何使用持续的数据在会话变量在控制器。

I have no idea as to what is the best way to implement this.OR How to persist data in the session variable. And how to use persisted data in the session variable in across the controller.

感谢您的帮助。

推荐答案

如果您正在使用ASP.NET窗体身份验证,用户名已经被存储在cookie中。您可以从控制器通过

If you are using ASP.NET Forms Authentication, the user name is already stored in a cookie. You can access it from the Controller via

Controller.User.Identity.Name

这是可能的用户ID存储作为用户名。当你调用像

It's possible to store the user ID as the user name. When you call something like

FormsAuthentication.RedirectFromLoginPage

给它的ID,而不是一个名字。该ID然后可以使用上面的方法来发现并没有多余的会话数据是必要的。如果你想存储的东西在会话中,只需拨打

Give it the ID instead of a name. The ID can then be found using the method above and no extra session data is necessary. If you want to store something in the session, just call

Session["UserID"] = value;

从控制器。

这篇关于如何在MVC3剃须刀视图中使用会话变量,以保持数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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