访问 Razor 布局中的会话变量?

pav*_*van 1 asp.net razor asp.net-mvc-4

当我在布局页面中编写代码时

轮廓

@Session["iname"].ToString();

我收到这个错误

Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)

我尝试了 @HttpContext.Current.Session["iname"].ToString() 但不起作用

小智 6

你可以在设计中做到这一点

@using Microsoft.AspNetCore.Http
@if (Context.Session.GetString("iname") != null){}
Run Code Online (Sandbox Code Playgroud)