Class 如何在带有Asp.NETCore2.0的MVC6中初始化和使用自定义/支持类 公共密封类SessionContext { 私有会话httpContext; 公共会话上下文(ISession httpContext) { this.httpContext=httpContext; } 公共字符串用户类型 { 得到 { 返回httpContext.GetString(“_UserType”); } 设置 { httpContext.SetString(“\u UserType”,value); } } ……更多属性。。。。。 } 公共类HomeController:控制器 { 私有应用设置_AppSettings; private SessionContext session=null; 专用只读IHttpContextAccessor\u httpContextAccessor; 私有ISession httpContext=>\u httpContextAccessor.httpContext.Session; //我不喜欢这个构造函数,因为它正在初始化或每次控制器调用。 公共HomeController(IOOptions myAppSettings、IHttpContextAccessor httpContextAccessor) { _appSettings=myAppSettings.Value; _httpContextAccessor=httpContextAccessor; appSettings=新的appSettings(_appSettings);//应该只初始化一次。 会话=新的会话上下文(httpContext); } }

Class 如何在带有Asp.NETCore2.0的MVC6中初始化和使用自定义/支持类 公共密封类SessionContext { 私有会话httpContext; 公共会话上下文(ISession httpContext) { this.httpContext=httpContext; } 公共字符串用户类型 { 得到 { 返回httpContext.GetString(“_UserType”); } 设置 { httpContext.SetString(“\u UserType”,value); } } ……更多属性。。。。。 } 公共类HomeController:控制器 { 私有应用设置_AppSettings; private SessionContext session=null; 专用只读IHttpContextAccessor\u httpContextAccessor; 私有ISession httpContext=>\u httpContextAccessor.httpContext.Session; //我不喜欢这个构造函数,因为它正在初始化或每次控制器调用。 公共HomeController(IOOptions myAppSettings、IHttpContextAccessor httpContextAccessor) { _appSettings=myAppSettings.Value; _httpContextAccessor=httpContextAccessor; appSettings=新的appSettings(_appSettings);//应该只初始化一次。 会话=新的会话上下文(httpContext); } },class,asp.net-core-mvc,helper,Class,Asp.net Core Mvc,Helper,我有关于……的问题 如何在带有Asp.NETCore2.0的MVC6中初始化和使用自定义/支持类 当我初始化这些类时,它们将初始化或每次控制器调用。这是非常多余的 我的SessionContext类每次都会重新初始化。因此,当我从另一个控制器调用这个类时,我失去了这些值 我尝试过这种方法,但没有多大用处。 services.AddSingleton() 从问题转移到回答: 现在,我可以共享我在支持类中设置的会话变量 public void ConfigureServices(IServiceCo

我有关于……的问题

  • 如何在带有Asp.NETCore2.0的MVC6中初始化和使用自定义/支持类 当我初始化这些类时,它们将初始化或每次控制器调用。这是非常多余的
  • 我的SessionContext类每次都会重新初始化。因此,当我从另一个控制器调用这个类时,我失去了这些值

    我尝试过这种方法,但没有多大用处。 services.AddSingleton()


  • 从问题转移到回答:

    现在,我可以共享我在支持类中设置的会话变量

    public void ConfigureServices(IServiceCollection services)
    {
       services.AddOptions();
       services.AddSingleton<SessionContext, SessionContext>(); 
    //calling the extension class to instantiate the classes which we require earlier.
       services.AddMyProjectHelper(Configuration)  
    }
    
    public SecurityController(MyProjectHelper objHelper, SessionContext sessionContext)
    { 
       session = sessionContext;
       projectHelper  = projectHelper  ?? objHelper; 
    }
    

    最后解决了这个问题。感谢ASP.NET核心第二部分中的博客强类型配置设置。。。。由Khalid Abuhakmeh撰写
    public static class MyProjectHelperExtensions
      {
            public static IServiceCollection AddMyProjectHelper(this IServiceCollection services, IConfiguration configuration)
            {
                var section = configuration.GetSection("AppSettings");
    
               // we first need to create an instance
                var settings = new AppSettings();
    
               // then we set the properties 
                new ConfigureFromConfigurationOptions<AppSettings>(section).Configure(settings); 
    
               var session = services.BuildServiceProvider().GetService<SessionContext>(); 
    
               // then we register the instance into the services collection
                services.AddSingleton<MyProjectHelper>(new MyProjectHelper(settings, session));
    
               return services;
            }
    }
    
    public SecurityController(MyProjectHelper objHelper, SessionContext sessionContext)
    { 
       session = sessionContext;
       projectHelper  = projectHelper  ?? objHelper; 
    }
    
    private SessionContext session = null;
    public HomeController(SessionContext sessionContext)
    {
       session = sessionContext;
    }
    
    [Authorize]
    public IActionResult Index()
    {
       if (session.CurrEmployee != null)
       {
          ViewBag.Name = (session.CurrEmployee.FirstName + " " + session.CurrEmployee.LastName);
          return View();
       }
    }