Asp.Net MVC: Error Handling Ajax Partial Views

During the second year of my job as a Application Developer I was ask to design some type of error handler to properly display partial views that were pulled in via ajax. What was currently happening was that partial errors were being displayed as full page error and causing other issues when Jquery libraries being loaded in twice.

Questions
  • Could you create a partial view and full view error page for display appropriately for both? So in the case where it was a partial view error the error page would simply display inside the partial.
  • Is there a way to determine by ActionResult type the error handler needed?
My Solution
 ///This is an attribute for PartialViews. It handles any errors and override the basecontroller's OnExpection method. 
    public class PartialErrorAttribute : HandleErrorAttribute
    {
        ///Similar setup to the base controller OnExpection method, but has a special partial view error page called _ErrorPartial".<summary>        ///This OnException will happen before the base controller OnException and tested for an unhandled exception when it reachs the base 
        /// controller.</summary>
        /// <param name="filterContext" />This filter includes the stack trace as well as any inner messages.
        public override void OnException(ExceptionContext filterContext)
        {
          if (filterContext.ExceptionHandled) return;
            HttpException hE = filterContext.Exception as HttpException;
            MasterModel master = new MasterModel()
             if (filterContext.Exception.InnerException != null)
             {
               master.Exception = filterContext.Exception.InnerException;
             }
             else
             {
               master.Exception = filterContext.Exception;
             }

             ///I believe this is the correct way of doing things...
             var path = filterContext.Controller.ControllerContext.RequestContext.HttpContext.Request.Url.PathAndQuery;
             var IPAddress = filterContext.Controller.ControllerContext.RequestContext.HttpContext.Request.UserHostAddress;
             var referer = filterContext.Controller.ControllerContext.RequestContext.HttpContext.Request.UrlReferrer.AbsolutePath;
             using (var ssc = new SiteService.SiteServiceClient())
             {
                 ssc.LogError(path, "Partial", IPAddress, referer, master.UserID, master.Global.CartCookieID, master.Exception.StackTrace, master.Exception.Message, filterContext.Controller.ControllerContext.RequestContext.HttpContext.Request.UserAgent, master.Global.PriorityCode);
             }
             filterContext.ExceptionHandled = true;
             filterContext.Result = new PartialViewResult { ViewName = "_ErrorPartial", ViewData = new ViewDataDictionary(master) };

            base.OnException(filterContext);
            }
        }

Resources

Popular posts from this blog

UI-Bootstrap Collapsible Sticky Footer

Installing Windows on Acer chromebook 15 cb3-532

Aspetcore: NLog with Postgresql