4

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.

3
  • a simple google search can give you this information, anyway, it can be accessed with request.httpcontext in controllers and with the word context in views Commented May 26, 2020 at 10:06
  • In the controller it works, but at the service level it does not work. Commented May 26, 2020 at 10:23
  • @user3296338 services.AddSession();app.UseSession(); added in startup class? Commented May 26, 2020 at 10:27

4 Answers 4

9

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();
    
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

1

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

1 Comment

In the controller it works, but at the service level it does not work
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.