开发者

Mvc3 RouteConstraint - Session is null

In Mvc3 I created an own RouteConstraint class as I implemented the IRouteConstraint interface, which means that I implemented the Match function. The only but serious problem I have that whenever the Match function is called, the session object is always null.

My simple code looks like:

public bool Match(HttpContextBase httpContext, Route route, string par开发者_如何学CameterName, 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;
}

I cannot avoid using the session object since I need the "level"/"type" of the logged-in administrator. I would not like to do my things in the cotroller class either, because it would be cumbersome to maintain by time.

Thanks, Gabor


I posted an answer to a similar question here on session states I battled with too if anyone else lands on this post. a BaseController class might be your answer too as the maintenance will be in 1 controller only and the rest will inherit:

session becoming null in MVC AuthorizeAttribute

Hope it helps someone in future!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜