2011-08-22 39 views
3

在Mvc3中,我創建了自己的RouteConstraint類,因爲我實現了IRouteConstraint接口,這意味着我實現了Match函數。我唯一但嚴重的問題是,無論何時調用Match函數,會話對象始終爲空。Mvc3 RouteConstraint - 會話爲空

我簡單的代碼看起來像:

public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) 
{ 
    if (routeDirection != RouteDirection.IncomingRequest) 
    { 
     return true; 
    } 

    HttpSessionStateBase sessionBase = httpContext.Session; // will be null 
    HttpSessionState session = HttpContext.Current.Session; // this will be null either 

    return true; 
} 

,因爲我需要的「級別」登錄的管理員/「型」我不能避免使用會話對象。我不想在cotroller課上做我的事情,因爲按時間維護會很麻煩。

感謝,的Gabor

+1

我有同樣的問題,自2011年8月22日以來你解決了你的問題嗎? –

+0

同樣的問題;有沒有人解決這個問題? –

回答

0

我在這裏貼在我對這個職位太多,如果別人的土地爭奪會話狀態的回答類似的問題。一個BaseController類可能是你的答案太作爲維護將是隻有1個控制器,其餘將繼承:

session becoming null in MVC AuthorizeAttribute

希望它可以幫助別人的未來!