user8906214
user8906214

Reputation:

MVC CORE setting a session

I've been following the Microsoft documentation trying to set a session using this line

HttpContext.Session.SetString(SessionKeyName, "Rick");

from the page https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?tabs=aspnetcore2x

but it gives the error

'ISession' does not contain a definition for 'SetString'

There's a Set option, but that takes a byte.

Any idea what I'm doing wrong and why I'm using the wrong HttpContext.Session?

Upvotes: 0

Views: 393

Answers (1)

Shyju
Shyju

Reputation: 218832

SetString,GetString,GetInt32 and SetInt32 are extension methods on ISession defined inside the Microsoft.AspNetCore.Http namespace. So to use these, you should add a using statement to this namespace in your class.

using Microsoft.AspNetCore.Http;

Now in your class, you can use these extension methods

HttpContext.Session.SetString("Test", "Rick");
HttpContext.Session.SetInt32("Age", 25);

Upvotes: 1

Related Questions