简体   繁体   中英

How to call Session variables in another project in the Same Solution Asp .NET Core 3

I am running multiple projects in my system, as shown below:

  1. Startup project is Project 1
  2. BLL Project (in this project I am setting the session)
  3. Common Project (in this project only I want to call the session)

In this class, I cannot create a constructor. This is my class:

namespace Project.Common
{
   public class Share
   {
        public const string SESSION_CURRENT_COMPANY = "Company";

        public static int LoggedInMembersCompanyID
        {
            get
            {    
                    var companyIdSession = HttpContext.Session.GetInt32(Common.SESSION_CURRENT_COMPANY);
                    int _companyIdSessionID = 0;
                    if (companyIdSession != null)
                    {
                        int.TryParse(companyIdSession.ToString(), out _companyIdSessionID);
                    }
                    return _companyIdSessionID;
            }
        }
    }
}
  1. You have to add this in the startup.cs Configure Function just before app.UsesEndpoits

    Common.Configure(app.ApplicationServices.GetRequiredService<IHttpContextAccessor>(), Configuration);

  2. Go to the Common Project and Go to Common.cs add the following thing at the top:

private static IHttpContextAccessor _httpContextAccessor; 
private static IConfiguration _configuration;
public static void Configure(IHttpContextAccessor httpContextAccessor,IConfiguration configuration)
{
                _httpContextAccessor = httpContextAccessor;
                _configuration = configuration;
}
  1. Get the Session value as shown below:
public static int LoggedInMembersCompanyID
{
  get
     { 
       var companyIdSession = HttpContext.Current.Session[Share.SESSION_CURRENT_COMPANY];
       int _companyIdSessionID = 0;
       if (companyIdSession != null)
       {
         int.TryParse(companyIdSession.ToString(), out _companyIdSessionID);
       }
        return _companyIdSessionID; 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM