如何使用会话在ASP.NET MVC应用4? [英] How to use sessions in an ASP.NET MVC 4 application?

查看:106
本文介绍了如何使用会话在ASP.NET MVC应用4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我新的ASP.NET MVC。我已经使用PHP之前,很容易根据当前的会话变量创建一个会话并选择用户记录。

I am new to ASP.NET MVC. I have used PHP before and it was easy to create a session and select user records based on the current session variables.

我已经到处找在互联网上的一个简单的一步一步的教程,可以告诉我怎么在我的C#ASP.NET MVC 4应用创建和使用会话。我想创建,我可以从任何地方访问我的控制器,并能使用这些变量在我的LINQ查询用户变量的会话。

I have looked everywhere on the Internet for a simple step-by-step tutorial that can show me how to create and use sessions in my C# ASP.NET MVC 4 application. I want to create a session with user variables that I can access from anywhere in my controllers and be able to use the variables in my LINQ queries.

- 谢谢提前!

推荐答案

尝试

//adding data to session
//assuming the method below will return list of Products

var products=Db.GetProducts();

//Store the products to a session

Session["products"]=products;

//To get what you have stored to a session

var products=Session["products"] as List<Product>;

//to clear the session value

Session["products"]=null;

这篇关于如何使用会话在ASP.NET MVC应用4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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