0

I have created custom exception handler in MVC 3 app, unfortunately it is never hit if exception is thrown. Do I miss something?

Custom exception filter

public class ExceptionFilter : IExceptionFilter
{
        public void OnException(ExceptionContext filterContext)
        {
         //Never executed
        }
} 

Registering filter in Global.asax.cx

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new ExceptionFilter()); //must be before HandleErrorAttribute
        filters.Add(new HandleErrorAttribute());
    }

Web.config

<customErrors mode="RemoteOnly" defaultRedirect="/error/default">
  <error statusCode="404" redirect="/error/pagenotfound" />
</customErrors>
Tomas
  • 17,551
  • 43
  • 152
  • 257
  • where is the exception thrown? – Darin Dimitrov Apr 23 '12 at 11:24
  • I have tried in two places. 1. Navigate to non existing web page. 2. I have separated thread where perform some calculations, if exception occurs there it is not handled by filter. – Tomas Apr 23 '12 at 11:32
  • Exception filters handle exceptions that are thrown inside controller actions. They cannot be used to handle global exceptions occurring in your entire ASP.NET application. – Darin Dimitrov Apr 23 '12 at 11:33
  • 1
    How to handle all exceptions in MVC app? – Tomas Apr 23 '12 at 11:33
  • In Application_Error: http://stackoverflow.com/a/5229581/29407. If you go that route make sure you have removed the `HandleErrorAttribute` global filter registration from your Global.asax. – Darin Dimitrov Apr 23 '12 at 11:34
  • The solution which you suggested to test do not work as I would like. The ErrorsController fail to render MVC Views, it renders only static web pages. – Tomas Apr 24 '12 at 06:11
  • there's nothing that prevents you from returning a view from those actions instead of static content: `return View();`. You could even pass view models to those error views. Also did you remove the `HandleErrorAttribute` registration? – Darin Dimitrov Apr 24 '12 at 06:15
  • I am already replaced with View() but it do not render Views. Have you tested the solution? – Tomas Apr 24 '12 at 06:16
  • Well, then you must have something else going on because I am using this code in many applications and it has always worked fine and returned correct views. – Darin Dimitrov Apr 24 '12 at 06:17
  • I have removed HandleErrorAttribute. Replaced return Content(...) with return View(). When I enter non existing URL the Http404 action is executed and View() is hit. But after that I get IIS7 error "HTTP Error 404.0 - Not Found". If I execute Action directly in browsre like this "http://localhost:2008/Error/Http404" the View rendered correctly. – Tomas Apr 24 '12 at 06:23
  • Do you have some custom errors setup in your web.config? If you are using IIS 7 try putting the following `` inside the `` section. – Darin Dimitrov Apr 24 '12 at 06:25
  • Please download demo project which I just created. http://www.filejumbo.com/Download/F0D541491B8CC561 Run it on Casini and hit any non existing Url, you will find that Action is executed but view is not rendered. Your solution do not work by default. Maybe you something modified in web.config or in another place and forgot to indicate it. – Tomas Apr 24 '12 at 06:39
  • it's normal that your 404 view is not rendered. You have an error in it. So you have an error in something that's supposed to handle an error :-) That's pretty bad. In your `Http404.cshtml` you wrote `Layout = "_Layout";` but obviously such file doesn't exist. You must write a correct path to your layout, for example `Layout = "~/Views/Shared/_Layout.cshtml";`. But that's already in your `_ViewStart.cshtml` so it's probably not worth repeating it. – Darin Dimitrov Apr 24 '12 at 15:07
  • Writing full layout do not help in this situation, the Http404 view is still not rendered. – Tomas May 03 '12 at 07:41

1 Answers1

0

If you are testing by navigating to a missing page, what really happens is it tries to resolve the controller and you get an unhandled exception that throws a 500. Add a and you should be able to trap the other errors as well.

Jeremy Likness
  • 7,531
  • 1
  • 23
  • 25