2

I follow this article to implement Custom Error Pages and Error Logging. The logging works fine. However, the custom error page doesn't display for ajax request when exception occured, it works for normal http full request, e.g.

http://localhost/MyApp/Report/Create/17/ , work, normal url request

http://localhost/MyApp/Report/CreateUser/17/json, NOT work, using Ajax

ASP.NET MVC HandleError Attribute, Custom Error Pages and Logging Exceptions

 public class BaseController : Controller
    {

 protected override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);

            _log.Error("Error Logging", filterContext.Exception);

            // Output a nice error page when customErrors's mode = On.
            if (filterContext.HttpContext.IsCustomErrorEnabled)
            {
                filterContext.ExceptionHandled = true;               
                this.View("Error").ExecuteResult(this.ControllerContext);
            }
        }
}

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            //Display custom error page using controller.OnException(), HandleErrorAttribute has to be disabled.
           //filters.Add(new HandleErrorAttribute());
        }

  protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);


        }
}

web.config

<customErrors mode="On">

    </customErrors>

Any idea would very much appreciated.

Pingpong
  • 7,681
  • 21
  • 83
  • 209
  • Maybe answers on this link will help: http://stackoverflow.com/questions/1171035/asp-net-mvc-custom-error-handling-application-error-global-asax?answertab=oldest#tab-top – jflaga May 17 '12 at 14:39

1 Answers1

0

AJAX request are treated different from standard HTML GET\POST request as the browser does not provide automatic rendering of the result. This would be true for any backend stack (not limited to ASP.Net MVC). So in this case even if response contains complete html for error page (which i doubt ASP.Net MVC does for AJAX calls), the browse would not automatically redirect to error page
The way to go about it would be to catch exception in AJAX and redirect to the error page from client side.

Chandermani
  • 42,589
  • 12
  • 85
  • 88
  • Thanks for your advice. Would it work if the mvc application returns 500 status code, and the UI layer is able to see that, and act it accordingly. – Pingpong Oct 25 '11 at 09:15
  • Anything other that 200 & 300 Http codes send by server are treated as error in context of AJAX call. Framework such as jQuery have error handlers defined for AJAX post where you can write redirect behavior – Chandermani Oct 25 '11 at 12:12