在输入API请求中发现未知属性时引发错误? [英] Throw error when unknown property found in input API request?

查看:66
本文介绍了在输入API请求中发现未知属性时引发错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在将带有JSON字符串的Rest Request作为过滤器传递给Web API,如下所示,

Currently I'm passing Rest Request with JSON string as filter to Web API as follows,

http://localhost:13825/api/values/get?filter={"Name":"abcdef","Address":"Something"}

我有一个像下面这样的课程,

I have a class like follows,

Public class Customer
{
  public string Name{get;set;}
  public string Address{get;set;}
}

当我使用以下代码将JSON字符串解析为类对象时,它工作正常,

When I use the following code to parse JSON string to a class object it works fine,

public string Get(Customer filter)
{

}

问题是,当我通过如下过滤器时,

The problem is, When I pass filter like follows,

filter={"Name":"abcdef","Adess":"Something"}

我的代码将 null 值分配给Customer类的 Address 属性,但是当在任何类属性中都找不到JSON字符串的属性时,我想抛出一个错误

my code assigns null value to Address property of Customer class but i wanted to throw an error when a property from JSON string is not found in any of the class property.

我们可以使用 MissingMemberHandling.Error ,但是当具有我们在class中定义的所有属性的其他属性时,它将引发错误.这里的问题是不同的,我不会每次都传递Address和Name属性.我可能会也可能不会同时通过.

We can use MissingMemberHandling.Error but it will throw error when additional properties with all the properties what we defined in class . Here the problem case is different , I wont pass Address and Name properties every time. I may or may not pass both.

所以我不能提供必需的财产.

So I cant give required property.

在输入的JSON字符串中发现未知属性时,我需要引发一个错误.

I need to throw an error when unknown property is found in input JSON string.

推荐答案

您可以设置 JsonSerializerSettings.MissingMemberHandling MissingMemberHandling.Error.

获取或设置在反序列化期间如何处理丢失的成员(例如JSON包含对象中不是成员的属性).

Gets or sets how missing members (e.g. JSON contains a property that isn't a member on the object) are handled during deserialization.

如果要全局执行此操作,对于所有控制器,只需将其添加到global.asax

If you want to do this globally, for all controllers, simply add this to global.asax

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.MissingMemberHandling = MissingMemberHandling.Error;

如果要针对特定​​的操作/控制器执行此操作,则必须实现IControllerConfiguration:

If you want to do this for a specific action/controller, then you have to implement IControllerConfiguration:

public class RejectUnrecognizedPropertiesAttribute : Attribute, IControllerConfiguration 
{
    public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
    {
        controllerSettings.Formatters.Remove(controllerSettings.Formatters.JsonFormatter);

        var formatter = new JsonMediaTypeFormatter
        {
            SerializerSettings = 
            {
                MissingMemberHandling = MissingMemberHandling.Error
            }
        };

        controllerSettings.Formatters.Add(formatter);
    }
}

只需将[RejectUnrecognizedProperties]属性应用于您的操作/控制器.

And simply apply the [RejectUnrecognizedProperties] attribute to your action/controller.

这篇关于在输入API请求中发现未知属性时引发错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆