/// <summary> /// 执行结果 /// </summary> /// <param name="context">控制器上下文</param> public override void ExecuteResult(ControllerContext context) { AjaxRequestResult obj = null; if (_obj is AjaxRequestResult) { obj = _obj as AjaxRequestResult; } else { obj = new AjaxRequestResult(); if (!_iserror) { obj.value = _obj; } else { obj.error = _errorInfo; } } var req = context.HttpContext.Request; var resp = context.HttpContext.Response; var setting = new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat }; var callback = req.QueryString["jsoncallback"]; if (String.IsNullOrWhiteSpace(callback)) { callback = req.QueryString["callback"]; } if (String.IsNullOrWhiteSpace(callback)) { resp.Write(JsonConvert.SerializeObject(obj, setting)); } else //jsonp { resp.Write(callback); resp.Write("("); resp.Write(JsonConvert.SerializeObject(obj, setting)); resp.Write(");"); } }
/// <summary> /// IExceptionFilter接口中的方法 /// </summary> /// <param name="filterContext"></param> public void OnException(ExceptionContext filterContext) { if (_handlerMethod == ExceptionHandlerMethod.ReturnJson) { var req = filterContext.RequestContext.HttpContext.Request; var resp = filterContext.RequestContext.HttpContext.Response; var callback = req.QueryString["jsoncallback"]; if (String.IsNullOrWhiteSpace(callback)) { callback = req.QueryString["callback"]; } var exp = filterContext.Exception as CustomException; var re = new AjaxRequestResult { error = new AjaxRequestErrorInfo { message = filterContext.Exception.Message.Trim(), errorCode = exp != null ? exp.ErrorCode : 0, errorType = exp != null ? exp.ExceptionType : string.Empty } }; resp.ContentEncoding = System.Text.Encoding.UTF8; resp.ContentType = "application/json"; resp.ClearContent(); var serializer = new JsonSerializer(); if (String.IsNullOrWhiteSpace(callback)) { serializer.Serialize(resp.Output, re); } else //jsonp { resp.Write(callback); resp.Write("("); serializer.Serialize(resp.Output, re); resp.Write(");"); } WriteExceptionLog(filterContext.Exception); filterContext.ExceptionHandled = true; } else if (_handlerMethod == ExceptionHandlerMethod.RedirectErrorPage) { var re = new ViewResult(); re.ViewName = "Error"; var exp = filterContext.Exception as CustomException; if (exp != null && !string.IsNullOrWhiteSpace(exp.ViewName)) { re.ViewName = exp.ViewName; } var errorMsg = filterContext.Exception.Message.Trim(); if (errorMsg.Contains("parameters")) { errorMsg = "非法的http请求"; } re.ViewBag.ErrorMsg = errorMsg; re.ViewBag.ErrorCode = exp != null ? exp.ErrorCode : 0; filterContext.Result = re; WriteExceptionLog(filterContext.Exception); filterContext.ExceptionHandled = true; } else if (_handlerMethod == ExceptionHandlerMethod.CallJavascriptFunction) { if (!String.IsNullOrWhiteSpace(_jsFunctionName)) { var buf = new System.Text.StringBuilder(); buf.AppendLine("<script type=\"text/javascript\">"); buf.AppendLine(String.Format(_jsFunctionName, filterContext.Exception.Message.Trim().ReplaceJsonEscapeChar())); buf.AppendLine("</script>"); var resp = filterContext.RequestContext.HttpContext.Response; resp.ClearContent(); resp.ContentEncoding = System.Text.Encoding.UTF8; resp.ContentType = "text/html"; resp.Write(buf.ToString()); WriteExceptionLog(filterContext.Exception); filterContext.ExceptionHandled = true; } } else if (_handlerMethod == ExceptionHandlerMethod.CustomErrorFormat1) { var resp = filterContext.RequestContext.HttpContext.Response; resp.ClearContent(); resp.ContentEncoding = System.Text.Encoding.UTF8; resp.ContentType = "text/html"; resp.Write("E,"); resp.Write(filterContext.Exception.Message.Trim()); WriteExceptionLog(filterContext.Exception); filterContext.ExceptionHandled = true; } }