我想在视图中显示Session.那可能吗?我在我看来尝试这个
<div class="content-header col-xs-12">
<h1>Welcome, @HttpContext.Session.GetString("userLoggedName")</h1>
</div>
Run Code Online (Sandbox Code Playgroud)
但我得到一个错误
严重级代码描述项目文件行抑制状态错误 CS0120 非静态字段,方法或属性"HttpContext.Session"需要对象引用
任何帮助,我将不胜感激.谢谢
我正在尝试在ASP.NET Core 2.1项目中的帮助器类中访问HttpContext.Session对象。
当我尝试访问HttpContext.Session时,出现以下错误。
CS0120非静态字段,方法或属性'HttpContext.Session'需要对象引用
使用.NET 4.x ASP.NET,可以使用“ HttpContext.Current.Session”轻松访问它。
这是我的课:
public class MySession
{
public void Foo()
{
HttpContext.Session.SetString("Name", "The Doctor"); // will not work
HttpContext.Session.SetInt32("Age", 773); // will not work
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的Startup.cs:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
// Set …Run Code Online (Sandbox Code Playgroud)