Custom filter in ASP.NET MVC 5 does not affect

816 views Asked by At

I have written this the below filter to log and handling exceptions in my MVC 5 app:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public sealed class HandleAndLogErrorAttribute : HandleErrorAttribute {

    public override void OnException(ExceptionContext filterContext) {
        if (filterContext == null)
            throw new ArgumentNullException("filterContext");
        if (filterContext.IsChildAction) {
            LogChildActionException(filterContext);
            return;
        }
        if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
            return;
        var exception = filterContext.Exception;
        if (!ExceptionType.IsInstanceOfType(exception))
            return;
        var controllerName = (string)filterContext.RouteData.Values["controller"];
        var actionName = (string)filterContext.RouteData.Values["action"];
        LogActionException(controllerName, actionName, exception);
        if (new HttpException(null, exception).GetHttpCode() != 500)
            return;
        var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
        var exceptionContext = filterContext;
        var viewResult = new ViewResult {
            ViewName = View,
            MasterName = Master,
            ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
            TempData = filterContext.Controller.TempData
        };
        exceptionContext.Result = viewResult;
        filterContext.ExceptionHandled = true;
        filterContext.HttpContext.Response.Clear();
        filterContext.HttpContext.Response.StatusCode = 500;
        filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
    }

    private void LogActionException(string controllerName, string actionName, Exception exception) {
        // log here
    }

    private void LogChildActionException(ExceptionContext filterContext) {
        // log here
    }
}

And I add this filter instead of the original one:

public class FilterConfig {
    public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
        filters.Add(new HandleAndLogErrorAttribute ());
    }
}

But it seems it does not affect at all:

public class TestController : Controller {

    // I even tried this line:
    // [HandleAndLogErrorAttribute]
    // and also this one:
    // [HandleAndLogErrorAttribute(ExceptionType = typeof(Exception))]
    public ActionResult Index() {
        try {
            var i = new[] { 1, 2, 3, 4 };
            var j = i[10];
        } catch {
            throw new HttpException();
            // I also tried:
            // throw new Exception();
            // and
            // throw;
        }
        return null;
    }
}

I did put a break-point at OnException() method. But it never get called. Do you know what is going on there? What am I missing? Thanks in advance.

Here is my other configs:

In Global.asax.cs

public class MvcApplication : System.Web.HttpApplication {
    void Application_Error(object sender, EventArgs e) {
        // I tried this line too:
        //Server.ClearError();
        // but nothing changed!
    }
}

In web.config

<system.web>

  <!-- I also tried false -->
  <trace enabled="true"/> 

  <!-- I also tried Off -->
  <customErrors mode="On" redirectMode="ResponseRedirect">
    <error statusCode="404" redirect="/404.html"/>
  </customErrors>

  <!-- I also tried true -->
  <compilation debug="false" targetFramework="4.5"/>

</system.web>

<system.webServer>

  <!-- I also tried Detailed -->
  <httpErrors errorMode="Custom">
    <remove statusCode="404"/>
    <error statusCode="404" path="/404.html" responseMode="ExecuteURL"/>
  </httpErrors>

</system.webServer>

And of-course, I tried to run application with/without debugging enabled and Debug/Release mode too.

0

There are 0 answers