Session null in ASP.Net MVC Controller Constructors

前端 未结 6 1714
北海茫月
北海茫月 2020-11-27 13:27

Why is Session null in the constructors of Controllers? It can be accessed from Action methods. Presumably, because the MVC Routing framework is responsible for newing-up a

6条回答
  •  猫巷女王i
    2020-11-27 14:00

    If you are using an IoC Container, try injecting and using the HttpSessionStateBase instead of the Session object:

    private static Container defaultContainer()
    {
        return new Container(ioc =>
        {
            // session manager setup
            ioc.For()
               .Use(ctx => new HttpSessionStateWrapper(HttpContext.Current.Session)); 
        });
    }
    

提交回复
热议问题