Asp.net-Mvc

如何在 ASP.NET MVC 4 應用程序中使用會話?

  • January 3, 2013

我是 ASP.NET MVC 的新手。我以前使用過 PHP,很容易創建會話並根據目前會話變數選擇使用者記錄。

我在 Internet 上到處尋找一個簡單的分步教程,它可以向我展示如何在我的 C# ASP.NET MVC 4 應用程序中創建和使用會話。我想創建一個包含使用者變數的會話,我可以從控制器中的任何位置訪問它,並且能夠在我的 LINQ 查詢中使用這些變數。

嘗試

//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;

引用自:https://stackoverflow.com/questions/14138872