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

dst*_*str 9 rest json.net asp.net-core-mvc asp.net-core

我们有ASP.NET Core应用程序用作REST api。方法[FromBody]MyClass param用作输入。问题是,如果客户端发送无效的JSON字符串,input则由于JSON序列化错误,该值将变为null。有没有办法记录这种错误?

编辑:我正在寻找不是按方法的应用程序范围的解决方案。

dst*_*str 7

我通过在Startup.cs中添加错误处理程序解决了此问题:

services.AddMvc()
        .AddJsonOptions(options => {
              options.SerializerSettings.Error = (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args) =>
              {
                    //Log args.ErrorContext.Error details...
              };
        });
Run Code Online (Sandbox Code Playgroud)