Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Session State in ASP.NET vNext MVC 6

Tags:

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.

like image 452
Manish Kumar Avatar asked Aug 01 '14 09:08

Manish Kumar


2 Answers

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.)

like image 200
Eilon Avatar answered Sep 17 '22 22:09

Eilon


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
like image 23
Ben Cull Avatar answered Sep 19 '22 22:09

Ben Cull