Exemple #1
0
        /// <summary>
        /// 获取处理后的返回结果。
        /// </summary>
        /// <param name="filterContext"></param>
        /// <returns></returns>
        protected virtual ActionResult GetHandledResult(ExceptionContext filterContext)
        {
            EmptyArrayResultAttribute attr = null;

#if !NETCOREAPP
            var serviceProvider = ContainerUnity.GetContainer();
            if (ActionContext.Current != null)
            {
                attr = ActionContext.Current.ActionDescriptor
                       .GetCustomAttributes <EmptyArrayResultAttribute>().FirstOrDefault();
            }
#else
            var serviceProvider = filterContext.HttpContext.RequestServices;
            var descriptor      = filterContext.ActionDescriptor as ControllerActionDescriptor;
            if (descriptor != null)
            {
                attr = descriptor.MethodInfo
                       .GetCustomAttributes(typeof(EmptyArrayResultAttribute), false)
                       .Cast <EmptyArrayResultAttribute>().FirstOrDefault();
            }
#endif
            if (attr != null)
            {
                //返回空数组,一般用在列表绑定上
                if (attr.EmptyArray)
                {
                    return(new JsonResultWrapper(new string[0]));
                }
                //使用提示信息
                else if (!string.IsNullOrEmpty(attr.Message))
                {
                    return(new JsonResultWrapper(Result.Fail(attr.Message)));
                }
            }

            var handler = serviceProvider.TryGetService <IExceptionHandler>();
            if (handler != null)
            {
                return(handler.GetResult(filterContext.Exception));
            }

            return(new JsonResultWrapper(Result.Fail("发生错误,请查阅相关日志或联系管理员。")));
        }
Exemple #2
0
        /// <summary>
        /// 获取处理后的返回结果。
        /// </summary>
        /// <param name="filterContext"></param>
        /// <returns></returns>
        protected virtual ActionResult GetHandledResult(ExceptionContext filterContext)
        {
            EmptyArrayResultAttribute attr = null;

#if !NETSTANDARD
            if (ActionContext.Current != null)
            {
                attr = ActionContext.Current.ActionDescriptor
                       .GetCustomAttributes(typeof(EmptyArrayResultAttribute), false)
                       .Cast <EmptyArrayResultAttribute>().FirstOrDefault();
            }
#else
            var descriptor = filterContext.ActionDescriptor as ControllerActionDescriptor;
            if (descriptor != null)
            {
                attr = descriptor.MethodInfo
                       .GetCustomAttributes(typeof(EmptyArrayResultAttribute), false)
                       .Cast <EmptyArrayResultAttribute>().FirstOrDefault();
            }
#endif
            if (attr != null)
            {
                //返回空数组,一般用在列表绑定上
                if (attr.EmptyArray)
                {
                    return(new JsonResultWrapper(new string[0]));
                }
                //使用提示信息
                else if (!string.IsNullOrEmpty(attr.Message))
                {
                    return(new JsonResultWrapper(Result.Fail(attr.Message)));
                }
            }

            return(new JsonResultWrapper(Result.Fail("发生错误,请查阅相关日志或联系管理员。")));
        }