Brian Brian - 2 months ago 10
ASP.NET (C#) Question

Application_Error not fired for 404 in MVC 3 when url contains 4 or more path segments

I am trying to create a custom error page for 404 errors. I have implemented the Application_Error method in Global.asax.cs. For the most part this works great, but the method isn't getting called for URLs with 4 or more path segments.

The specific URL that I'm having trouble with is:

http://localhost/Area/Controller/Action/ID


If I remove the ID, my custom 404 age loads fine. The problem seems to be having 4 levels in my path (/Area/Controller/Action/ID). Any ideas?

Answer

I suspect your routes don't account for a URL with 4 parts. Try adding a catch all route as your last route to test this:

routes.MapRoute("Error404", "{*url}", new {controller = "Error", action = "PageNotFound" } );

Including (some of) your routes in your RouteConfig.cs question might also help.

Comments