I created an ASP.NET MVC 5 application by adding the following code snippets:
Web.config
<system.web>
<customErrors mode="On">
<error statusCode="404" redirect="~/Erro/Erro404"/>
</customErrors>
</system.web>
Global.asax.cs
protected void Application_Error(object sender, EventArgs e)
{
if (Response.StatusCode != 404) //Condição para ignorar erros 404.
{
Exception ex = Server.GetLastError();
Response.Clear();
Logger.Registrar(ex.ToString()); //Método que registra o erro em um arquivo de log.
Server.ClearError();
Response.Redirect("~/Erro/Desconhecido");
}
}
As I already set up an action for 404 errors, I want them to pass right through Application_Error
, thus leading the user to ~/Erro/Erro404
.
The problem: 404 errors always enter if
in Application_Error
, because the server returns a status code 200 (OK), when it should be 404 (Not Found).
Does anyone know where the error is?