using System; using System.Net; using System.Text; using System.Web.Mvc; using FHT.Agency.CommonTypes; using FHT.Agency.Domains.Exceptions; using FHT.Agency.Exceptions; using FHT.Agency.Exceptions.Abstract; using FHT.Core.Exceptions.Abstract; namespace FHT.Agency.Web.Filters { public class AgencyMvcExceptionFilter : FilterAttribute, IExceptionFilter { public void OnException(ExceptionContext filterContext) { if (!filterContext.ExceptionHandled && filterContext.Exception is FhtAgentBaseException) { bool isAjaxRequest = filterContext.RequestContext.HttpContext.Request.IsAjaxRequest(); if (isAjaxRequest) { // There was an weird error caused by IIS7.5 modifying HTTP 400 response body. // Either add the below line or add: to in Web.config filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; filterContext.Result = new JsonResult() { ContentEncoding = Encoding.UTF8, ContentType = "application/json", Data = new { Message = filterContext.Exception.Message, }, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; filterContext.ExceptionHandled = true; } else { /*TODO ApplicationEnvironment.CurrentEnvironment == ApplicationEnv.Production*/ if (false) { filterContext.Result = new ViewResult() { ViewName = "Error", ViewData = new ViewDataDictionary(filterContext.Exception), }; filterContext.ExceptionHandled = true; } } } #if DEBUG if (!(filterContext.Exception is FhtAgentBaseException) && filterContext.Exception.GetType().FullName.ToLower().Contains("fht.agency") && !(filterContext.Exception is AuditRejectedExceptionBase) ) { throw new NoExtendsFhtAgencyBaseException(string.Format("{0}没有继承FhtAgencyBaseException", filterContext.Exception.GetType().FullName)); } #endif } } }