如何记录ASP.NET Core输入JSON序列化错误

编程入门 行业动态 更新时间:2024-10-28 08:18:40
本文介绍了如何记录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序列化错误

本文发布于:2023-11-13 23:25:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585605.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   序列化   NET   ASP   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!