Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I would create a dynamic routing in my ASP.Net – MVC Project (Framework 4.5.1). To do this I create a custom MvcRouteHandler which work fine until I tried to add a new condition in base of a User-Session-Value. Unfortunately I’ve noticed, that the Sessions in the Routing-Class always are null. Is there any way to get the Session-Value already in the Routing-Handler – or is that another way to store User-related Data which I can access in the Routehandler too?

Here's my Code - Routeconfig.cs:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{*FriendlyUrl}"
        ).RouteHandler = new myRouteHandler();
    }

...and (the simplified) myRouteHandler.cs:

public class myRouteHandler : MvcRouteHandler, IRequiresSessionState
{
    protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        String myController = "Home";
        String myAction = "Index";
        Int32 myRouteId = 0;

        //here i would try to get a Session-Value, but Session is always null...
        var mySession = requestContext.HttpContext.Session["mySession"];

        if (mySession != null)
        {
            //do something different e.g.
            myAction = "About";
        }

        requestContext.RouteData.Values["controller"] = myController;
        requestContext.RouteData.Values["action"] = myAction;
        requestContext.RouteData.Values["id"] = myRouteId;

        return base.GetHttpHandler(requestContext);
    }
}
share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.