ASP.NET CORE和NEWTONSOFT JSON.NET:如何强制在无效字段上失败?
我们有一个.NET Core 3.1 Web应用程序,并使用newtonsoft JSON.NET序列化器注册了用于序列化/进行序列化的序列化。但是现在我们有愚蠢的客户向我们发送了无效的JSON,其中包含我们在模型中没有的字段。 JSON忽略它们是正常的,但是当Deserialializer发现无效字段时,我需要找到创建模型状态错误的方法。
我们对可选字段有问题,因此默认验证在这里无济于事。
We have a .NET Core 3.1 web app and use Newtonsoft JSON.NET serializer there registered for serializing/deserializing. But now we got stupid client who send us invalid JSON which contain fields which we don't have in our models. It's normal for JSON to ignore them, but I need to find way to create model state errors when deserializer finds invalid fields.
We have problem with optional fields so default validation won't help here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基于此 doc doc
尝试类似此
错误处理程序的内容:
args.errcontext.handled = {true/false};
based on this doc
try something like this
error handler:
args.ErrorContext.Handled = {true/false};
找到的解决方案
当我的模型没有JSON的字段时, 将引发错误。命名不好,我认为这会反之亦然。
found solution
will throw error when my model doens't have fields from json. Bad naming I was thinking that it'll work vice versa.