简体   繁体   English

如何在同一解决方案 Asp .NET Core 3 中的另一个项目中调用 Session 变量

[英]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启动项目是项目 1
  2. BLL Project (in this project I am setting the session) BLL 项目(在这个项目中我正在设置会话)
  3. Common Project (in this project only I want to call the session)公共项目(在这个项目中只有我想调用会话)

In this class, I cannot create a constructor.在这个 class 中,我无法创建构造函数。 This is my class:这是我的 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您必须在 app.UsesEndpoits 之前在 startup.cs 中添加此配置 Function

    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: Go 到 Common Project 和 Go 到 Common.cs 在顶部添加以下内容:

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:获取Session值如下图:
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; 
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Src asp.net 内核从同一解决方案中的另一个项目获取图像? - how to get image from another project in the same solution with Src asp.net core? Asp.Net Core v1.1从同一解决方案中的另一个项目获取继承的类名 - Asp.Net Core v1.1 Get Inherited Class Names From another project in same solution ASP.NET Core:从解决方案中的另一个项目访问appsettings - ASP.NET Core: Accessing appsettings from another project in solution .NET 核心重定向到 Controller 在同一解决方案下的另一个项目中 - .NET Core Redirect to Controller in another Project under same Solution 在同一解决方案中将一个项目重定向到另一个项目Asp.net-如何指定路径 - Redirecting one project to another project in same solution Asp.net-How to specify path 在同一个解决方案中结合 ASP.NET 和 ASP.NET 核心 - Combine ASP.NET and ASP.NET core in the same solution 在同一个Asp.Net Core 2解决方案中将DbContext注入到类库项目中 - Injecting DbContext to class library project in same Asp.Net Core 2 solution 是否可以在另一个解决方案中从 ASP.NET Webforms 项目中引用 ASP.NET Core 2.0 类库 dll? - Possible to reference an ASP.NET Core 2.0 class library dll from an ASP.NET Webforms project in another solution? 如何从 ASP.NET Core 中的另一个项目返回视图? - How to return view from another project in ASP NET Core? 从另一个项目访问文件与ASP.NET MVC相同的解决方案 - Access files from another project same solution asp.net mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM