Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 826
user11334 Avatar asked Sep 16 '08 08:09

user11334


3 Answers

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.

like image 151
Steve Morgan Avatar answered Nov 20 '22 05:11

Steve Morgan


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.

like image 29
Haacked Avatar answered Nov 20 '22 03:11

Haacked


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

like image 2
Garry Shutler Avatar answered Nov 20 '22 03:11

Garry Shutler