protected StandardJsonResult JsonError(string errorMessage) { var result = new StandardJsonResult(); result.AddError(errorMessage); return(result); }
protected StandardJsonResult JsonErrorAllowGet(string errorMessage) { var json = new StandardJsonResult(); json.AddError(errorMessage); json.JsonRequestBehavior = JsonRequestBehavior.AllowGet; json.MaxJsonLength = 1000000; return(json); }
protected StandardJsonResult JsonValidationError() { var result = new StandardJsonResult(); foreach (var validationError in ModelState.Values.SelectMany(v => v.Errors)) { result.AddError(validationError.ErrorMessage); } return(result); }
protected StandardJsonResult JsonError(string errorMessage, JsonRequestBehavior behavior = JsonRequestBehavior.DenyGet) { var result = new StandardJsonResult { JsonRequestBehavior = behavior }; result.AddError(errorMessage); return(result); }
public override void OnException(ExceptionContext filterContext) { if (filterContext == null) { throw new ArgumentNullException(nameof(filterContext)); } if (!filterContext.IsChildAction && !filterContext.ExceptionHandled && filterContext.HttpContext.IsCustomErrorEnabled && new HttpException(null, filterContext.Exception).GetHttpCode() == (int)HttpStatusCode.InternalServerError && this.ExceptionType.IsInstanceOfType(filterContext.Exception)) { if (filterContext.HttpContext.Request.IsAjaxRequest()) { var jsonResult = new StandardJsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet }; jsonResult.AddError(filterContext.Exception.Message); filterContext.Result = jsonResult; } else { var controllerName = (string)filterContext.RouteData.Values["controller"]; var actionName = (string)filterContext.RouteData.Values["action"]; var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName); var viewData = new ViewDataDictionary <HandleErrorInfo> { Model = model }; filterContext.Controller.ViewData.ForEach(viewData.Add); filterContext.Result = new ViewResult { ViewName = this.View, MasterName = this.Master, ViewData = viewData, TempData = filterContext.Controller.TempData }; } filterContext.ExceptionHandled = true; filterContext.HttpContext.Response.Clear(); filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; } }
public static JsonResult JsonError( this Controller controller, string errorMessage, string contentType = null, Encoding contentEncoding = null, JsonRequestBehavior jsonRequestBehavior = JsonRequestBehavior.DenyGet) { var result = new StandardJsonResult { ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = jsonRequestBehavior, }; result.AddError(errorMessage); return(result); }
public static JsonResult JsonValidation( this Controller controller, string contentType = null, Encoding contentEncoding = null, JsonRequestBehavior jsonRequestBehavior = JsonRequestBehavior.DenyGet) { var result = new StandardJsonResult { ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = jsonRequestBehavior, }; var validationMessages = controller.ModelState.Values.SelectMany(v => v.Errors.Select(e => e.ErrorMessage)); foreach (var validationMessage in validationMessages) { result.AddError(validationMessage); } return(result); }