コード例 #1
0
        public override void OnException(ExceptionContext filterContext)
        {
            var response = filterContext.HttpContext.Response;

            // in allow debug mode just show standard ASP.NET error
            if (ShowErrorPageInDebugMode && filterContext.HttpContext.IsDebuggingEnabled)
            {
                base.OnException(filterContext);
                return;
            }

            var ex = filterContext.Exception.GetBaseException();

            CallbackErrorResponseMessage resultMessage = new CallbackErrorResponseMessage(ex, AllowExceptionDetail);

            response.ContentType = "application/json";
            response.StatusCode  = 500;
            var cbEx = ex as CallbackException;

            if (cbEx != null && cbEx.StatusCode > 0)
            {
                response.StatusCode = ((CallbackException)ex).StatusCode;
            }

            response.Write(JsonConvert.SerializeObject(resultMessage));

            filterContext.ExceptionHandled  = true;
            response.TrySkipIisCustomErrors = true;
        }
コード例 #2
0
        /// <summary>
        /// Returns a JSON error response to the client
        /// </summary>
        /// <param name="ex">Exception that generates the error message and info to return</param>
        /// <param name="statusCode">Optional status code</param>
        /// <returns></returns>
        public JsonResult ReturnAjaxError(Exception ex, int statusCode = 500)
        {
            Response.Clear();
            Response.StatusCode = statusCode;
            var cb = new CallbackErrorResponseMessage(ex, HttpContext.IsDebuggingEnabled);

            return(Json(cb, JsonRequestBehavior.AllowGet));
        }