gpt4 book ai didi

c# - 如何在 ASP.NET 5 MVC6 中获取布局文件中的 session 值

转载 作者:太空狗 更新时间:2023-10-29 22:15:51 24 4
gpt4 key购买 nike

在 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。

最佳答案

ContextHttpContext 之间的命名有些困惑。您可以使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com