Mvc3 RouteConstraint - Session is null

588 Views Asked by At

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 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;
}

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

1

There are 1 best solutions below

0
On

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!