5

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!

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

Comments

7

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.

1 Comment

This should be the answer because the intent is to secure the Index action method of the PagesController.
2

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.