简体   繁体   English

如何在MVC 3 Web应用程序中使用ASP.NET Session对象?

[英]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 除了拥有ViewDataViewBag还可以存储在控制器和视图之间

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.

相关问题 如何在MVC中使用ASP.NET Web窗体? - How do I use ASP.NET Web Forms in MVC? 如何为 ASP.NET MVC Web 应用程序中使用的图像添加缓存? - How do I add caching for images used in my ASP.NET MVC web app? 如何在ASP.Net Web表单中模拟/伪造会话对象? - How do I mock/fake the session object in ASP.Net Web forms? 如何在强类型 ASP.NET MVC 创建和编辑视图的 DropDownList 中使用实体对象导航属性? - How Do I Use Entity Object Navigation Properties In A DropDownList On My Strongly Typed ASP.NET MVC Create and Edit Views? 如何将ASP.NET MVC Web应用程序的标识/身份验证部分移到类库中? - How can I move the identity/authentication part of my ASP.NET MVC web app into a class library? 如何在 ASP.NET MVC 中使用 Automapper 10.1.1 - How do I use Automapper 10.1.1 in ASP.NET MVC Web API controller? 什么是ASP.NET中的会话,我该如何使用它? - What is a session in ASP.NET, and how do I use it? 如何在.NET 3.5 Web应用程序中安装和使用ASP.NET AJAX Control Toolkit? - How do I install and use the ASP.NET AJAX Control Toolkit in my .NET 3.5 web applications? 如何在我的ASP.NET MVC应用程序中使用jQuery选项卡进行导航? - How can I use jQuery tabs for navigation in my ASP.NET MVC app? 如何向我的 MVC asp.net Core 应用程序添加排序功能? - How do I add a sorting feature to my MVC asp.net Core app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM