Reputation: 23645
i'm using an example in which i can fake the session. It's for use in a unittest of a mvc controller.
In the test i create a controller and then i do this:
FakeHttpContext httpctx = new FakeHttpContext(null,null,null,null,mSessionItems );
ControllerContext ctx2 = new ControllerContext(httpctx,new RouteData(), target);
where mSessionItems
is my session and target
is my instance of a controller
and indeed, when i'm in a controller reading this.ControllerContext.HttpContext.Session
, i have a session, great!
but.... i also read the session outside the controller, and there i use HttpContext.Current.Session
, and that is null (or actualy, the HttpContext.Current
is null).
So i wondered, what is the difference between the two?
Upvotes: 2
Views: 2935
Reputation: 364409
ControllerContext.HttpContext is of the abstract type HttpContextBase. Default implementation of this type (HttpContextWrapper) wraps access to HttpContext. So when you create the fake implementation you are replacing its relation to HttpContext. Fake implementation will not create real HttpContext for you.
Btw. where do you access the session outside controller? How do you know that there will be any current HttpContext?
Upvotes: 2