本文介绍了如何记录ASP.NET Core输入JSON序列化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们有ASP.NET Core应用程序,我们将其用作REST api.方法使用[FromBody]MyClass param作为输入.问题是,如果客户端发送无效的JSON字符串,由于JSON序列化错误,input值将变为null.有没有记录这种错误的方法?
We have ASP.NET Core application which we use as a REST api. Methods use [FromBody]MyClass param as input. Problem is if the client sends an invalid JSON string the input value becomes null due to JSON serialization errors. Is there a way to log such errors?
我正在寻找应用程序范围内的解决方案,而不是按方法进行..
I'm looking for application-wide solution not per-method..
推荐答案我已通过在Startup.cs中添加错误处理程序解决了此问题:
I've solved this with adding an error handler in Startup.cs:
services.AddMvc() .AddJsonOptions(options => { options.SerializerSettings.Error = (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args) => { //Log args.ErrorContext.Error details... }; });更多推荐
如何记录ASP.NET Core输入JSON序列化错误
发布评论