Esempio n. 1
0
        public override void OnException(ExceptionContext filterContext)
        {
            filterContext.ExceptionHandled = true;                                               //表示已经处理 不需要mvc框架处理
            var         UserData      = HttpContext.Current.User as MyFormsPrincipal <UserInfo>; //当前操作用户
            UsersExtend curentUser    = null;
            var         routeValueDic = filterContext.RouteData.Values;
            string      actionParameterInfo;

            try
            {
                actionParameterInfo = GetActionParametersInfoStr(filterContext);
            }
            catch
            {
                actionParameterInfo = "参数信息:不能处理参数绑定";
            }
            if (UserData != null)
            {
                curentUser = DepositHelper.GetAsync <UsersExtend>(RedisKeyManager.LoginUserInfo(UserData.UserData.Id.ToString()));
            }
            //如果是ajax请求则返回json格式数据  否则跳转到错误页面
            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                JsonResult json = new JsonResult();
                json.Data = new ProcessResult()
                {
                    State = ProcessEnum.Fail, Message = filterContext.Exception.Message
                };
                filterContext.Result = json;
            }
            else
            {
                filterContext.Result = new RedirectResult("/plug/Error/Error404.html");
            }
            log.Error(ClientIP.GetIp(filterContext.RequestContext.HttpContext.ApplicationInstance.Request), curentUser == null ? "未登陆" : curentUser.LoginName, filterContext.HttpContext.Request.UrlReferrer.ToString(), routeValueDic["action"].ToString(), "mvc全局捕获异常", filterContext.Exception, actionParameterInfo);//写入日志
            base.OnException(filterContext);
        }