0

i have two folder under view folder. one is Home and that has index.aspx file another folder in view folder called DashBoard and that has MyDash.aspx

my routing code look like in global.asax

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

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

        routes.MapRoute(
        "DashBoard", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "DashBoard", action = "MyDash", id = UrlParameter.Optional } // Parameter defaults
        );
    }

so when i type url like http://localhost:7221/ or http://localhost:7221/Home then index.aspx is being render from Home folder but when i type url like http://localhost:7221/DashBoard then page not found is coming but if i type like http://localhost:7221/DashBoard/MyDash then page is coming.

so what is wrong in my second routing code . why MyDash.aspx is not coming when i type url like http://localhost:7221/DashBoard. what is wrong?

what i need to change in my second routing code??

please have a look.....i am new in MVC. thanks

My UPDATE

when i change route entry in global.asax file then it started working. can u please explain why....

            routes.MapRoute(
                "DashBoard",
                "DashBoard/{action}/{id}",
                 new { controller = "DashBoard", action = "MyDash", id = UrlParameter.Optional }
            );

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

can i write routing code this way

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

same pattern for two url....please discuss in detail. thanks

5
  • "DashBord" should be "DashBoard"? Commented Aug 4, 2012 at 20:06
  • i fix the spelling but still no luck. i think route name is not important that it could be anything. Commented Aug 4, 2012 at 20:11
  • DashBoard/{action}/{id} matches any URL that begins with "DashBoard". If no second parameter is passed in, it defaults to "MyDash" for the action, so http://localhost/Dahsboard works. If a route does not begin with "DashBoard", this pattern is not matched, then the next route-mapping is inspected for a match. This next route-mapping is so general that anything with 0 to 3 route parameters (e.g. /foo or /foo/bar or /foo/foo/bar) will get caught by it. Commented Aug 5, 2012 at 9:35
  • No your re-writing of the route in the update will not work. The 4th parameter would negate the 3rd. The 4th parameter in this overload of the method sets constraints on what values can be accepted, so this mapping will only match routes with "DashBoard" and one with "Home" in it simply will not get matched at all. So the default controller specified in the 3rd parameter will be completely ignored in all cases. Commented Aug 5, 2012 at 9:44
  • ...to re-phrase, your 3rd parameter (in your re-write) would be saying use "Home" if no controller is passed in by the URL, but your 4th paramter is saying this URL must pass in a controller name of "DashBoard" as the first part of the URL, else there's no match here at all -- and it will move on and check the next route mapping, set up by the next routes.MapRoute for a match. Commented Aug 5, 2012 at 9:49

1 Answer 1

1

The route names (1st parameter) have no impact on what action/controller gets invoked.

Your 2 route patterns, however, (2nd paramters of routes.MapRoute) are identical :

"{controller}/{action}/{id}"

... so anything that would be matched by the 2nd pattern gets caught by the first pattern. Therefore they're all getting mapped by the first map definition.

http://localhost:7221/Home works because it matches the first pattern, and presumably, the Index action exists inside your Home controller.

http://localhost:7221/DashBoard/MyDash works because, even though it's getting matched by the 1st route, it overrides the default action/controller (Home/Index) by the route parameters passed in through the URL (DashBoard/MyDash).

http://localhost:7221/DashBoard doesn't work because it's getting picked up by the first route pattern, but you didn't pass in an action name, so it looks for the default -- Index -- which I'm guessing you haven't set up within the DashBoard controller.

UPDATE (how to fix the problem):
So if you want http://localhost:7221/DashBoard to map to Controller named DashBoard with an action named MyDash, while still allowing other patterns to be picked up by {controller}/{action}/{id} delete your 2nd route, and place this one as the 1st route:

routes.MapRoute(
        "DashBoard", 
        "DashBoard/{action}/{id}",
         new { controller = "DashBoard", action = "MyDash", id = UrlParameter.Optional }               
    );

This is a more specific route, so it needs to go before the catch-all {controller}/{action}/{id}. Nothing that doesn't start with /DashBoard will get picked up by it.

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

1 Comment

See my comments under your original question for details on why this works, and why your suggestion (in your update) won't work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.