2009-07-29 49 views
3

有人可以給我一個例子或鏈接到在asp.net mvc中使用會話之一嗎?asp.net mvc session example

+1

我想它的工作原理就像在web表單如何添加庫。 – 2009-07-29 11:36:07

回答

10

與WebForms中的相同:

使用HttpContext.Current.Session對象。

HttpContext.Current.Session["UserName"] = "Jon"; 

string userName = HttpContext.Current.Session["UserName"]; 
+0

您提前10秒發佈! ^^ – 2009-07-29 11:37:12

0

隨着MVC Future庫是可以做到的要簡單得多

Global.asax.cs添加一行在Application_Start()方法

ValueProviderFactories.Factories.Add(new SessionValueProviderFactory()); 

然後你就可以在控制器通過

$Session["YourName"] = "some value"; 

訪問會話或在查看

@Session["YourName"] 

here is article到你的項目