示例#1
0
        public override void OnException(ExceptionContext context)
        {
            context.HttpContext.Request.ContentType       = "application/json";
            context.HttpContext.Request.Headers["Accept"] = "application/json";
            ReJson model = new ReJson(5001, "执行错误:" + context.Exception.Message);

            context.Result = new JsonResult(model);
            base.OnException(context);
        }
示例#2
0
        public async Task ExceptionHandle(HttpContext context, Exception exception)
        {
            string message = exception.Message;

            ReJson error = new ReJson(message);

            context.Response.ContentType = "text/json";
            await context.Response.WriteAsync(error.ToJson());
        }
示例#3
0
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (!context.ModelState.IsValid)
            {
                var error = string.Empty;

                foreach (var key in context.ModelState.Keys)
                {
                    var state = context.ModelState[key];
                    if (state.Errors.Any())
                    {
                        error = string.Join("\r\n", state.Errors.ToList().ConvertAll(item => item.ErrorMessage));
                        if (error.Equals(string.Empty))
                        {
                            error = string.Join("\r\n", state.Errors.ToList().ConvertAll(item => item.Exception.Message));
                        }
                        break;
                    }
                }

                ReJson re = new ReJson(40000, error);
                context.Result = new ContentResult()
                {
                    Content     = re.ToJson(),
                    ContentType = "application/json"
                };
                //throw new Exception(error);
            }

            var notVali = ValiSignature(context.HttpContext);

            if (notVali != null)
            {
                // 过滤器不要抛出错误,也不要向响应流写东西,应该直接赋值Result进行短路
                context.Result = new ContentResult()
                {
                    Content     = notVali.ToJson(),
                    ContentType = "application/json"
                };
            }
        }