How can I make HandleErrorAttribute work with Ajax?

后端 未结 1 1445
北荒
北荒 2021-01-18 20:15

In my ASP.NET MVC 2 application I use HandleErrorAttribute to display a custom error page in case of unhandled exceptions, and it works perfectly unless the exception happen

相关标签:
1条回答
  • 2021-01-18 20:29

    To achieve this you could write a custom action filter:

    public class AjaxAwareHandleErrorAttribute : HandleErrorAttribute
    {
        public string PartialViewName { get; set; }
    
        public override void OnException(ExceptionContext filterContext)
        {
            // Execute the normal exception handling routine
            base.OnException(filterContext);
    
            // Verify if AJAX request
            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                // Use partial view in case of AJAX request
                var result = new PartialViewResult();
                result.ViewName = PartialViewName;
                filterContext.Result = result;
            }
        }
    }
    

    And then specify the partial view to be used:

    [AjaxAwareHandleError(PartialViewName = "~/views/shared/error.ascx")]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    
        public ActionResult SomeAction() 
        {
            throw new Exception("shouldn't have called me");
        }
    }
    

    And finally in your view assuming you have the following link:

    <%= Ajax.ActionLink("some text", "someAction", new AjaxOptions { 
        UpdateTargetId = "result", OnFailure = "handleFailure" }) %>
    

    You could make the handleFailure function to update the proper div:

    <script type="text/javascript">
        function handleFailure(xhr) {
            // get the error text returned by the partial
            var error = xhr.get_response().get_responseData();
    
            // place the error text somewhere in the DOM
            document.getElementById('error').innerHTML = error;
        }
    </script>
    
    0 讨论(0)
提交回复
热议问题