有人能给我一个在ASP.NET MVC中使用会话的例子或链接吗?
有人能给我一个在ASP.NET MVC中使用会话的例子或链接吗?
和在WebForms中一样:
使用HttpContext.Current.Session对象。
HttpContext.Current.Session["UserName"] = "Jon";
string userName = HttpContext.Current.Session["UserName"];
使用MVC Future库可以更简单地完成此操作
在Global.asax.cs
文件的Application_Start()
方法中添加一行代码
ValueProviderFactories.Factories.Add(new SessionValueProviderFactory());
然后您可以通过以下方式在控制器中访问会话:
$Session["YourName"] = "some value";
或者在视图中
@Session["YourName"]
这里是文章 如何将库添加到您的项目中