2
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");
        }
    }
Since I’ve already set an action for 404 errors, I want these to go right through Application_Error, thus leading the user to ~/Erro/Erro404.
The problem: 404 errors always enter the if in Application_Error, because the server returns a status code 200 (OK), when it should be 404 (Not Found).
Can anyone tell where the mistake is?
I put the
Response.TrySkipIisCustomErrors = true;insideApplication_Error()and theResponse.StatusCode = 404;at the beginning of the view~/Erro/Erro404. The view now returns 404, but only if it is accessed directly. The rest hasn’t changed a bit. This is how I should do it?– tkpb
When you access an invalid path what happens?
– Bacco
received the view with code 200 and the parameter
?aspxerror=/endereco-errado-aquiat the end of the address.– tkpb
IIS 8.0. Is on Azure Websites.
– tkpb
@tkpb updated the answer
– Bacco