[英]How do I use ASP.NET Session object in my MVC 3 web app?
I need to use session in my MVC 3 web app like I used to do in my old ASP.NET web app. 我需要在MVC 3 Web应用程序中使用会话,就像以前在旧的ASP.NET Web应用程序中一样。 I need to save some values, and then access it from any of my controllers and all other views.
我需要保存一些值,然后从我的任何控制器和所有其他视图访问它。
Please give me a very simple example following the best practices. 请按照最佳做法给我一个非常简单的示例。
I am new in MVC development and started with MVC 3. 我是MVC开发的新手,从MVC 3开始。
You use session the same way you do in webforms. 使用会话的方式与在Webforms中使用的方式相同。
In your controllers you can simply use the Session
object. 在您的控制器中,您可以简单地使用
Session
对象。 You could save the state of a model in Session
and pass the result to your view. 您可以将模型的状态保存在
Session
然后将结果传递给视图。
Basic session
usage remains same for both mvc and webforms. mvc和webforms的基本
session
用法保持不变。 In addition to that you have ViewData
, ViewBag
also to store between controller and view 除了拥有
ViewData
, ViewBag
还可以存储在控制器和视图之间
Refer to this article for creating a helper class for using strongly typed sessions in mvc. 请参阅此文章为MVC使用强类型会话创建一个辅助类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.