作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在 ASP.NET 5 MVC 6 Beta 8 中,我需要在我的 _Layout.cshtml
中读取 session 变量,或者获取对当前 HttpContext 的引用。
请注意:在 ASP.NET 5 中,引用 Session 对象与 ASP.net 4 相比发生了显着变化,详见 this。问题
在 Beta7 和 Beta8 之间,Context
对象也被重命名为 HttpContext
。
在我当前的 Controller 中,我目前像这样保存 session 变量
public IActionResult Index()
{
HttpContext.Session.SetInt32("Template", (id));
}
在我的 _Layout.cshtml 中,我需要读取上面的 session 变量。我需要以某种方式引用当前的 HttpContext 例如
HttpContext.Current.Session.GetInt32("Template");
但我不知道如何在 cshtml 文件中获取当前的 HttpContext。
最佳答案
Context
和HttpContext
之间的命名有些困惑。您可以使用 Context
属性访问 View 中的 HttpContext:
@{ int x = Context.Session.GetInt32("test"); }
MVC 存储库中还有一个与此相关的悬而未决的问题:https://github.com/aspnet/Mvc/issues/3332
关于c# - 如何在 ASP.NET 5 MVC6 中获取布局文件中的 session 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33212908/
我是一名优秀的程序员,十分优秀!