views:

237

answers:

2

I have a route table similar to this

// Allow browsing categories by name, instead of by ID.
routes.MapRoute("Categories", "Categories/{action}/{name}",
 new { controller = "Categories", action = "Index", name = "" }
);

// All other pages use the default route.
routes.MapRoute("Default", "{controller}/{action}/{id}",
 new { controller = "Applications", action = "Index", id = "" }
);

// Show a 404 error page for anything else.
routes.MapRoute("Error", "{*url}",
 new { controller = "Error", action = "404" }
);

However the wildcard route at the end is not being invoked... if I type in a controller or URL that does not exist it attemps the default route above the wildcard route... thus causing the infomous resource not found ASP.net error page... I want a custom error page... please help

+1  A: 

If you type in a controller that does not exist, such as 'BadController/SomeAction/123', it will get picked up by the second route.

That last route will only get picked up if neither of the previous two do, such as a URL of '/SomeRandomFile.aspx', or something else that doesn't even come close to the '{controller}/{action}/{id}' syntax.

In my projects I use the <customErrors mode="RemoteOnly" defaultRedirect="/Error" /> inside my web.config file as a 'catch-all' rule for exceptions. Assuming you don't have a rule to route a request to '/Error', you'll need an Error controller.

You can then use the protected void Application_Error(object sender, EventArgs e) method to execute some code in the case of an exception.

JMs
+1  A: 

Why don't you create standard ASP .NET custom error page?

<customErrors
       mode="RemoteOnly" 
       defaultRedirect="~/View/Shared/CustomErrorPasge.aspx" 
/>
emirc