菜鸟笔记 ASP Core 请求参数验证

        public IActionResult Create([FromBody]CreateOrderDto dto)
        {
            if (ModelState.IsValid)
            {
                //TODO:...
            }
        }

   public class AjxPage
    {
        public int Code { get; set; }

        public long Count { get; set; }

        public dynamic Data { get; set; }

        public string Msg { get; set; }
    }

public class XcActionFilter : IActionFilter
{
    public void OnActionExecuting(ActionExecutingContext context)
    {
        if (!context.ModelState.IsValid)
        {
            XcHttpResult result = new XcHttpResult() { Result = false };

            foreach (var item in context.ModelState.Values)
            {
                foreach (var error in item.Errors)
                {
                    result.Msg += error.ErrorMessage + "|";
                }
            }

            context.Result = new JsonResult(result);
        }
    }

    public void OnActionExecuted(ActionExecutedContext context)
    {

    }
}



public void ConfigureServices(IServiceCollection services)
{

    services.AddMvc(options =>
    {
        options.Filters.Add<XcActionFilter>();
    });
}

  

posted @ 2019-07-21 16:37  哈啾小包子  阅读(173)  评论(0编辑  收藏  举报