I'm trying to learn ASP.NET MVC and have a few questions about routing.
Print is a controller, with a Show action, that takes a parameter and returns it back as string.
Consider the code
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
name: "Print",
url: "Print/{action}/{id}",
defaults: new { controller = "Print", action = "Show", id = UrlParameter.Optional });
}
Why do I get a 404 error when I try host:xxxxx/Print/xxx...? Shouldn't it take me to the Show action?
Also if I set url:Print, and try host:xxxxx/Print I get the same error. It should take me to the Show action.
Similarly if I set url:Print/{action}/{id} and try host:xxxxx/Print/Show it gives the same error, even though the parameter is optional, and should return blank?
But if I interchange the two routes such that the Print route is first in precedence and Home/Index in second, I do not get any errors in any cases? host:xxxxx/Print shows blank, host:xxxxx/Print/Show shows blank and host:xxxxx/Print/Show/xxx... returns some value.
Why do I get errors if I set it as the second route?