Savvas Sopiadis
Savvas Sopiadis

Reputation: 8223

Wrong route selected in ASP.NET MVC 3

Could please someone shed some light into this issue, it drives me crazy!

The routes:

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

    routes.MapRoute(
     "DefaultOrderingRoute", // Route name
     "{controller}/{action}/{id}/{slug}", // URL with parameters
     new { controller = "Order", slug = UrlParameter.Optional }, // Parameter defaults
     new { controller = "^Order$" }
    );

    routes.MapRoute(
      "DefaultImageRoute", // Route name
      "{controller}/{action}/{id}", // URL with parameters
      new { controller = "Image", }, // Parameter defaults
      new { controller = "^Image$" }
    );

    routes.MapRoute(
      "FooterRoute", //route for invoking actions for the Footer
      "{controller}/{action}", // URL with parameters
      new { controller = "Footer", }, // Parameter defaults
      new { controller = "^Footer$" }
    );

    routes.MapRoute(
       "DefaultDealRoute", // Route name
       "{city}/{category}/{id}/{slug}", // URL with parameters
       new { category = Deals.Globals.Global.CATEGORY_ALL_NAME, controller = "Deal", action = "Details", slug = UrlParameter.Optional }, // Parameter defaults
       new { controller = "^Deal$", id = @"\d+" }
    );

    routes.MapRoute(
        "DealRouteForCategory", // Route name
        "{city}/{category}", // URL with parameters
        new { city = "", category = Deals.Globals.Global.CATEGORY_ALL_NAME, controller = "Deal", action = "Details" }, // Parameter defaults   
        new { controller = "^Deal$" }
    );
}

I have add a reference to routedebugger to see what is going on under the hood. So for the selected URL:

http://my.SERVER.IP/VirtualDirectoryName/Order/PayPalNotify/9/blabla

the URL debugger shows the following:

AppRelativeCurrentExecutionFilePath: ~/Order/PayPalNotify/9/adfaf (exactly what i have expected)!!!

The debugger also shows that routes DefaultOrderingRoute and DefaultDealRoute are matched (this i didn't expect! since i have constraints on the routes!!).

It also shows that the matched route is:

Matched Route: {controller}/{action}/{id}/{slug}

with route data:

controller:  Deal
action:      Details
id:          9
slug:        adfaf
city:        Athens
category:    All

How is this possible?? What am i doing wrong?

PS. I've noticed that by reissuing the URL the correct route gets executed!!!

Upvotes: 0

Views: 494

Answers (1)

Savvas Sopiadis
Savvas Sopiadis

Reputation: 8223

Mystery solved: i was doing something very bad(!):

in Session_Start() i had somewhere code which did the following (amongst others)

    Session_Start() 
    {
// code snippet out for brevity 
//setup the city route values
    HttpContext.Current.Request.RequestContext.RouteData.Values["city"] = cityToBeginWith;
//setup the category route values
    HttpContext.Current.Request.RequestContext.RouteData.Values["category"] = Deals.Globals.Global.CATEGORY_ALL_NAME;
//setup the controller route values
    HttpContext.Current.Request.RequestContext.RouteData.Values["controller"] = "Deal";
//setup the action route values
    HttpContext.Current.Request.RequestContext.RouteData.Values["action"] = "Details";    
    }

This caused the selection of the "false" route... very bad!!

Upvotes: 1

Related Questions