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

查看:31
本文介绍了如何在 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.

我在 Internet 上到处寻找一个简单的分步教程,该教程可以向我展示如何在我的 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天全站免登陆