How to implement Session State in ASP.NET vNext MV

2019-01-11 22:41发布

In Visual Studio 2014, ASP.NET vNext, i am trying to implement Session State in MVC 6.I am not getting any Intellisense in Visual Studio to implement it.Please suggest me how to use it.

3条回答
仙女界的扛把子
2楼-- · 2019-01-11 23:00

Just install the package

Install-Package Microsoft.AspNet.Session

and then put

app.UseSession();

in your Startup.cs. As Eilon pointed out, it will be available via

Context.Session.SetString("key", stringValue);
查看更多
我命由我不由天
3楼-- · 2019-01-11 23:24

Update 11/2/2014

The ASP.NET team has started building a new session state middleware to enable session state in ASP.NET vNext. You can check out the Session repo, which has both the Session middleware, as well as a sample.

To enable session state in an app, call:

app.UseSession();

And to read/write from it:

var some_value = context.Session.GetInt("some_value").Value;
some_value++;
context.Session.SetInt("some_value", some_value);

Original answer

Basically same question as How to do server side state management in vNext Web Applications - session state is not yet implemented in ASP.NET vNext.

As others have noted, TempData is not the same thing as Session State, it is merely built on top of it. (And it is also not yet implemented in ASP.NET vNext.)

查看更多
老娘就宠你
4楼-- · 2019-01-11 23:26

I've written a blog post outlining the details of How to Implement Sessions in ASP.NET 5, MVC6. Updated for Beta8

It boils down to:

  • Add Microsoft.AspNet.Session nuget package.
  • Add services.AddSession() to startup.cs
  • Add services.AddCaching() to startup.cs
  • Add app.UseSession() to startup.cs
  • Use HttpContext.Session inside controllers
  • Inject IHttpContextAccessor to get to the HttpContext outside of controllers
查看更多
登录 后发表回答