标签:conf class 异常 dict orm ptr ide filter app
1.cshtml页面异常
2.Controller异常
3.路由参数异常。
4.页面不存在404
页面不存在404,可以通过配置config来处理
<customErrors mode="On"> <error statusCode="404" redirect="~/Error/Page404" /> </customErrors>
其它的异常都可以通过HandleErrorAttribute拦截处理
public class ErrorFilter : HandleErrorAttribute { public override void OnException(ExceptionContext filterContext) { string errorMsg; var exception = filterContext.Exception; errorMsg = exception.Message; if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest()) { var accept = filterContext.RequestContext.HttpContext.Request.AcceptTypes; if (accept.Contains("application/json")) { filterContext.Result = new JsonResult() { Data = new { Success = false, Msg = errorMsg } }; } else { filterContext.Result = new JavaScriptResult() { Script = "alert( ‘" + errorMsg + "‘);" }; } } else { //普通异常 filterContext.Result = new ViewResult() { ViewName = "/Views/Test/ErrorMsg.cshtml",ViewData = new ViewDataDictionary() { { "msg", errorMsg } } }; } filterContext.ExceptionHandled = true; } }
注册全局过滤器
public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new ErrorFilter()); } }
标签:conf class 异常 dict orm ptr ide filter app
原文地址:http://www.cnblogs.com/lgxlsm/p/7903478.html