user3296338
user3296338

Reputation: 87

How to get HttpContext.Current.Session in ASP.NET Core?

I need to migrate an MVC project to .net Core, I know it has System.Web removed from ASP.net Core. I need to convert HttpContext.Current.Session ["name"]! = Null at asp.net core. I added: using Microsoft.AspNetCore.Http but I have an error.

Upvotes: 4

Views: 24517

Answers (4)

YASODHA
YASODHA

Reputation: 41

you don't have System.Web.HttpContext.Current.Session in ASP.NET Core. To access session in non-controller class step1: First, register the following service in Startup.ConfigureServices;

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

step2: register a class (example - TestOrder) where you want to access the Session in

Startup.ConfigureServices;
services.AddScoped<TestOrder>();

Now, in TestOrderclass, add the following code.

private readonly IHttpContextAccessor _httpContextAccessor;    
private readonly ISession _session;    
public TestOrder(IHttpContextAccessor httpContextAccessor)    
   {    
        _httpContextAccessor = httpContextAccessor;    
        _session = _httpContextAccessor.HttpContext.Session;    
    }

The above code is receiving IHttpContextAccessor object through dependency injection and then, it is storing Sessions in a local variable.

Upvotes: 4

Always_a_learner
Always_a_learner

Reputation: 1304

Use like this:

HttpContext.Session.SetString("priceModel", JsonConvert.SerializeObject(customobject));
var priceDetails = HttpContext.Session.GetString("priceModel");

Make sure below points in startup class:

  1. AddSession in ConfigureServices method

    services.AddSession();
    
  2. Usesession in configure method:

    app.UseSession();
    

Upvotes: 9

mj1313
mj1313

Reputation: 8479

The recommended approach is to register a dependency using the built-in dependency injection container. Inject IHttpContextAccessor into the corresponding service.

public class UserRepository : IUserRepository
    {
        private readonly IHttpContextAccessor _httpContextAccessor;

        public UserRepository(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }

        public void LogCurrentUser()
        {
            var username = _httpContextAccessor.HttpContext.Session.GetString("UserName");
            service.LogAccessRequest(username);
        }
    }

For more details, refer to this link

Upvotes: 1

Shojaeddin
Shojaeddin

Reputation: 2073

Did you test Microsoft doc, a sample is like below

public const string SessionKeyName = "_Name";
public const string SessionKeyAge = "_Age";
const string SessionKeyTime = "_Time";

 // Requires: using Microsoft.AspNetCore.Http;
    if (string.IsNullOrEmpty(HttpContext.Session.GetString(SessionKeyName)))
    {
        HttpContext.Session.SetString(SessionKeyName, "The Doctor");
        HttpContext.Session.SetInt32(SessionKeyAge, 773);
    }

    var name = HttpContext.Session.GetString(SessionKeyName);
    var age = HttpContext.Session.GetInt32(SessionKeyAge);

Upvotes: 1

Related Questions