Exemplo n.º 1
0
 /// <summary>
 /// Action方法执行前参数校验
 /// </summary>
 /// <param name="context"></param>
 public override void OnActionExecuting(ActionExecutingContext context)
 {
     if (!context.ModelState.IsValid)
     {
         var modelStateErrors = new Dictionary <string, IEnumerable <string> >();
         foreach (var state in context.ModelState)
         {
             var key    = state.Key;
             var errors = state.Value.Errors;
             if (errors != null && errors.Count > 0)
             {
                 var errorMessages = errors.Select(error =>
                 {
                     return(error.Exception != null ? error.Exception.Message : (String.IsNullOrEmpty(error.ErrorMessage) ? "An error has occurred " : error.ErrorMessage));
                 });
                 if (errorMessages.Count() > 0)
                 {
                     modelStateErrors.Add(key, errorMessages);
                 }
             }
             var response = new Response <Dictionary <string, IEnumerable <string> > >();
             response.SetMessage(ResponseStatusCode.RequestParameterIsWrong);
             response.Result = modelStateErrors;
             context.Result  = new ContentResult()
             {
                 StatusCode = (int)HttpStatusCode.BadRequest,
                 Content    = response.SerializeToJson()
             };
         }
         LogErrorHelper.LogError("OnActionExecuting", context.HttpContext, context.Result);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Action方法执行后异常处理,避免直接抛出异常
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            if (context.Exception.IsNotNull())
            {
                var response = new Response();
                response.HandleException(context.Exception);

                context.Result = new ContentResult()
                {
                    StatusCode = (int)HttpStatusCode.BadRequest,
                    Content    = response.SerializeToJson()
                };

                LogErrorHelper.LogError("OnActionExecuted", context.HttpContext, context.Result);
            }
        }
Exemplo n.º 3
0
        public void OnException(ExceptionContext context)
        {
            var response = new Response();

            response.HandleException(context.Exception);

            var serializerSettings = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };

            var content = JsonConvert.SerializeObject(response, Formatting.None, serializerSettings);

            context.Result = new ContentResult()
            {
                StatusCode = (int)HttpStatusCode.OK,
                Content    = content
            };
            context.ExceptionHandled = true;

            LogErrorHelper.LogError("OnException", context.HttpContext, context.Result);
        }