I'm trying to catch "all route" which doesnt have a defined map and redirect to a custom page. This way:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "PageNotFound",
url: "{*url}",
defaults: new { controller = "Home", action = "Error" }
);
}
If I load page that exist in the controller/action domain, it works (i.e. Default run correct). If instead i try to type somethings like "www.mywebsite.com/asd", it doesnt catch the PageNotFound route rules, but instead the framework trigger:
protected void Application_Error()
{
}
Where am I wrong? I want to avoid Application_Error error trigger, and just route to a custom page if (for example) a controller doesnt' exist.
EDIT Code edited after a comment:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "MyAppName.Controllers" }
).DataTokens["UseNamespaceFallback"] = false;
routes.MapRoute(
name: "PageNotFound",
url: "{*url}",
defaults: new { controller = "Home", action = "Error" },
namespaces: new string[] { "MyAppName.Controllers" }
).DataTokens["UseNamespaceFallback"] = false;
Still the same problem.