2

I've read many articles and several post (including here in stackoverflow) but do not know what I'm doing wrong.

Here my code:

Global.asax.cs

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
}

ErrorControler.cs

public class ErrorController : Controller
{
    public ActionResult Error404()
    {
        return View();
    }

    public ActionResult Error500()
    {
        return View();
    }
}

Web.config

<customErrors mode="On" defaultRedirect="~/Error/Error500">
  <error statusCode="404" redirect="~/Error/Error404"/>
</customErrors>

MyController.cs

public ActionResult Index()
{
    using (var db = new DataContext())
    {
        int a = 2, b = 0;
        var r = a / b;
        return View(r);
    }
}

Error500.cshtml

@model System.Web.Mvc.HandleErrorInfo
@{
    ViewBag.Title = "Erro";
}

<h2>@ViewBag.Title</h2>
<div id="error-info">
    <p>Ocorreu um erro inexperado na página <a class="error-url" href="@Response["aspxerrorpath"]" title="Origem do erro">@Response["aspxerrorpath"]</a></p>
    <p>Se o erro persistir, por-favor, entre em contato com os administradores do site.</p>
    <div class="error-details">
        <p>@Model.Exception.Message</p>
    </div>
</div>

When I try to access the path /MyController the following message appears:

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Error/Error500

I would like that happen when an error on any controller, if the http status code has not been informed in web.config, it redirects to the default view Error500

In this article, for example, it handles DbException errors, but would like to handle any type of error.

Errors of type 404 (page not found) works perfectly. The user is redirected to the page Error404.cshtml

pnuts
  • 58,317
  • 11
  • 87
  • 139
ridermansb
  • 10,779
  • 24
  • 115
  • 226

1 Answers1

8

If you want this to happen remove/comment the following line from your Global.asax:

filters.Add(new HandleErrorAttribute());

You basically have to choose whether you want ASP.NET to handle your errors (the <customErrors> section in your web.config) or ASp.NET MVC (the global HandleErrorAttribute action filter, which by the way requires you to turn on custom errors in web.config)

Or checkout an alternative method for handling errors in ASP.NET MVC (with this approach you still have to remove the line I showed from your Global.asax).

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I commented the line `filters.Add(new HandleErrorAttribute())`, but still continued the same behavior. This link is one of the links I found while researching the topic, but would not want to use the `Application_Error`. I would use the `HandleErrorAttribute` to handle the errors of my application. – ridermansb Jul 25 '11 at 21:07
  • @Riderman de Sousa Barbosa, I have done exactly the steps you described in a new ASP.NET MVC 3 project, removed the custom errors line as shown in my answer, added what you have shown in web.config, modified the Index action of HomeController to match what you have shown and it worked perfectly fine for me. – Darin Dimitrov Jul 25 '11 at 21:09
  • I discovered the error! For some reason, the error was occurring on the link, `@Response["aspxerrorpath"]` I commented this line and it worked perfectly. I found this out when I saw the error log the following message: Cannot apply indexing with [] to an expression of type 'System.Web.HttpResponseBase' – ridermansb Jul 25 '11 at 22:44