我使用了一個可以僞造會話的例子。 它用於mvc控制器的單元測試。ControllerContext.HttpContext.Session和HttpContext.Current.Session之間的區別是什麼
在測試中我創建了一個控制器,然後我做到這一點:
FakeHttpContext httpctx = new FakeHttpContext(null,null,null,null,mSessionItems);
ControllerContext ctx2 = new ControllerContext(httpctx,new RouteData(), target);
其中mSessionItems
是我的會議和target
是我的一個控制器的實例
,事實上,當我在控制器閱讀this.ControllerContext.HttpContext.Session
,我有一個會議,太棒了!
但是....我也讀過控制器外部的會話,並且在那裏我使用了HttpContext.Current.Session
,這是空的(或實際上,HttpContext.Current
爲空)。
所以我想知道,兩者有什麼區別?
我會得到HttpContext.Current.Session(當然我會檢查null)。但在單元測試場景中,我會卡住。 – Michel 2010-11-23 19:40:55