c# – How to customize error pages in an ASP.NET MVC system?


How to display a friendlier page when an error occurs in my asp.net mvc application (and not that yellow page)?


There is a special view for these cases, Shared/Error.cshtml (it has to be this name).

Just turn on your application's customErrors in Web.config :

    <customErrors mode="On" />

If you want to display error details like action name , controller name or exception , you can configure your view to receive a model of type HandleErrorInfo .

The only problem is, unfortunately, this solution doesn't handle 404 errors (page not found). Asp.net mvc does not easily support this type of error.

To handle it you'll need to write your own action for it (your own URL). Example, for the ~/erros/nao-encontrado URL:

<customErrors mode="On">
  <error statusCode="404" redirect="~/erros/nao-encontrado" />

In this case you are given the original URL via query string:

Scroll to Top