Advertisement
Advertisement


Asp.net MVC routing ambiguous, two paths for same page


Question

I'm trying out ASP.NET MVC routing and have of course stumbled across a problem. I have a section, /Admin/Pages/, and this is also accessible through /Pages/, which it shouldn't. What could I be missing?

The routing code in global.asax:

public static void RegisterRoutes(RouteCollection routes)
{
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            "Pages",    // Route name
            "Admin/Pages/{action}/{id}",  // URL with parameters
            // Parameter defaults
            new { controller = "Pages", action = "Index", id = "" }  
        );

        routes.MapRoute(
            "Default",   // Route name
            "{controller}/{action}/{id}",   // URL with parameters
             // Parameter defaults
            new { controller = "Home", action = "Index", id = "" }  
        );

    }

Thanks!

2009/04/03
1
5
4/3/2009 7:12:18 AM

Accepted Answer

I'd suggest adding an explicit route for /Pages/ at the beginning.

The problem is that it's being handled by the Default route and deriving:

controller = "Pages" action = "Index" id = ""

which are exactly the same as the parameters for your Admin route.

2008/09/16
7
9/16/2008 8:36:06 AM


You could add a constraint to the default rule so that the {Controller} tag cannot be "Pages".

2008/09/16

You have in you first route {action} token/parameter which gets in conflict with setting of default action. Try changing parameter name in your route, or remove default action name.

2008/09/17