views:

1318

answers:

4

Hi! 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!

+6  A: 

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.

Steve Morgan
+2  A: 

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

Garry Shutler
A: 

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.

Hrvoje
A: 

For routing issues like this, you should try out my Route Debugger assembly (use only in testing). It can help figure out these types of issues.

P.S. If you're trying to secure the Pages controller, make sure to use the [Authorize] attribute. Don't just rely on URL authorization.

Haacked