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到你的项目