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

大兔子大兔子 提交于 2019-12-05 18:26:30
Hector Correa

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.

  1. Open IIS, select your website, and choose the option "Error Pages".
  2. Find the 404 status code, right click and go to "Edit Feature Settings".
  3. Now, select "Custom Error pages" and change the "Path Type" to URL "Execute URL"
  4. Finally, put in your URL as a relative value, and now it should redirect to this url on 404 errors.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!