Exemple #1
0
        protected static ActionResult GetErrorResult(ActionExecutedContext filterContext, string actionName,
                                                     string formName)
        {
            string errorContent;

            try
            {
                filterContext.HttpContext.Response.Clear();
                filterContext.HttpContext.Response.StatusCode             = 500;
                filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;

                var friendlyMessageHandler = new zAppDev.DotNet.Framework.Utilities.ExceptionHandler();
                var msgObject = friendlyMessageHandler.HandleException(filterContext.Exception);
                errorContent = Newtonsoft.Json.JsonConvert.SerializeObject(msgObject,
                                                                           new Newtonsoft.Json.JsonSerializerSettings
                {
                    PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects
                });
            }
            catch (Exception e)
            {
                LogManager.GetLogger(typeof(zAppDev.DotNet.Framework.Utilities.ExceptionHandler))
                .Error("Could not produce friendly message for exception!", e);
                errorContent = filterContext.Exception.Message;
            }
            var controller = ((CustomControllerBase)filterContext.Controller);

            if (controller.Request.IsAjaxRequest())
            {
                return(new JsonResult
                {
                    Data = new
                    {
                        Type = "Error",
                        Data = errorContent,
                        filterContext.Exception.StackTrace,
                        RedirectURL = $"{formName}/{actionName}"
                    },
                    MaxJsonLength = int.MaxValue,
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            return(new RedirectToRouteResult(new RouteValueDictionary
            {
                ["action"] = actionName,
                ["controller"] = formName
            }));
        }
        private static ActionResult GetErrorResult(ActionExecutedContext context, string actionName,
                                                   string formName)
        {
            string errorContent;

            try
            {
                //context.HttpContext.Response.Clear();
                context.HttpContext.Response.StatusCode = 500;
                //context.HttpContext.Response.TrySkipIisCustomErrors = true;
                var friendlyMessageHandler = new zAppDev.DotNet.Framework.Utilities.ExceptionHandler();
                var msgObject = friendlyMessageHandler.HandleException(context.Exception);
                errorContent = Newtonsoft.Json.JsonConvert.SerializeObject(msgObject,
                                                                           new Newtonsoft.Json.JsonSerializerSettings
                {
                    PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects
                });
            }
            catch (Exception e)
            {
                LogManager
                .GetLogger(typeof(zAppDev.DotNet.Framework.Utilities.ExceptionHandler))
                .Error("Could not produce friendly message for exception!", e);
                errorContent = context.Exception.Message;
            }
            var controller = (CustomControllerBase)context.Controller;

            if (controller.Request.IsAjaxRequest())
            {
                return(controller.Json(new
                {
                    Type = "Error",
                    Data = errorContent,
                    StackTrace = context.Exception.StackTrace,
                    RedirectURL = $"{formName}/{actionName}"
                }));
            }
            return(new RedirectToActionResult(actionName, formName, new
            {
                action = actionName,
                controller = formName
            }));
        }