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.
-
What do you mean by "implement session state". Session state is part of the framework, it has already been implemented. Are you trying to _use_ session state? If so, what is not working? – Rune Aug 01 '14 at 10:01
-
@Rune,Yeah i am trying to use Session state in MVC 6, and it's not working for me.Means i am not getting Session Property in MVC 6.Like i get ViewBag,ViewData,Cookies etc. in Visual studio 2014 intelisense, But not getting Session.Please tell me how can we make use of it? Thanks. – Manish Kumar Aug 04 '14 at 09:24
-
I would advise that you don't implement "session state" using application constructs of ASP.NET. Make your site RESTful and persist any session information via content on your pages themselves and your database. – dkoch74 Aug 05 '14 at 20:43
-
@ManishKumar I have updated my answer given that ASP.NET vNext has a new session state middleware you can check out. – Eilon Nov 03 '14 at 03:48
3 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.)
-
-
@Nick-ACNB thanks for the heads up! We moved the source code from the Caching repo to a new Session repo so I updated the links in this post. – Eilon Feb 19 '15 at 01:56
-
I think you should update your answer since `context`is now written using a capital **C** `Context`and the `GetInt`and `SetInt` function do not exist anymore – RPDeshaies Apr 15 '15 at 01:08
-
@Tareck117 where are you seeing that it should be capital C Context? And I still see GetInt/SetInt (though there's a discussion about whether they should be changed to GetInt32/SetInt32, but no change has been made). – Eilon Apr 15 '15 at 01:18
-
Well...I said that because I included the `Microsoft.AspNet.Session` package and using the answer from @Michael Matejko and I was not able to see the context in my controller without using `Context` and the `Context.Session`property is a `ISessionCollection`. But from your comment I guess I'm doing this the wrong way... :) – RPDeshaies Apr 15 '15 at 02:18
-
@Tareck117 the `context` could be from anywhere, so I deliberately made it lowercase because it's not from a specific property or parameter. If it's in an MVC action then it may very well be `Context` but there are cases outside of MVC. For the GetInt/SetInt extension methods make sure you import the `Microsoft.AspNet.Http` namespace from the `Microsoft.AspNet.Http.Extensions` package. And BTW here's a cool site where you can find out in which package a type/method is located: http://packagesearch.azurewebsites.net/?q=getint – Eilon Apr 15 '15 at 05:49
-
You were right, I had not included the `Microsoft.AspNet.Http.Extensions` package and not imported the `Microsoft.AspNet.Http` namespace. Thanks ! – RPDeshaies Apr 15 '15 at 11:15
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

- 9,434
- 7
- 43
- 38
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);

- 163
- 2
- 9
-
I do not have access to Context in my startup.cs, what namespace does it reside in? – Nick-ACNB Feb 18 '15 at 14:12
-