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
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 :
… 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/Homeworks because it matches the first pattern, and presumably, the Index action exists inside your Home controller.http://localhost:7221/DashBoard/MyDashworks 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/DashBoarddoesn’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/DashBoardto 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:This is a more specific route, so it needs to go before the catch-all
{controller}/{action}/{id}. Nothing that doesn’t start with/DashBoardwill get picked up by it.