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?
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.
Set the custom error page for IIS: In IIS rightclick your website/virtual directory --> properties --> custom errors tab --> set the 404 error to your error page.
Your custom errors will only fire for things that fit your route rules. Once all of your route rules fail and it does not find a page at that path it passes the error up to IIS. IIS then uses its 404 rule for object not found so you have to override that and point it back into your project to your custom 404.
Are you getting your IIS error page instead? Try this.
- Open IIS, select your website, and choose the option "Error Pages".
- Find the 404 status code, right click and go to "Edit Feature Settings".
- Now, select "Custom Error pages" and change the "Path Type" to URL "Execute URL"
- Finally, put in your URL as a relative value, and now it should redirect to this url on 404 errors.
来源:https://stackoverflow.com/questions/6990677/application-error-not-fired-for-404-in-mvc-3-when-url-contains-4-or-more-path-se