WebAPI -在服务器端不正确反序列化的对象数组。

时间:2022-02-19 16:46:23

In the client-side, I am using AngularJS and in the server-side I am using ASP.NET WebAPI.

在客户端,我使用AngularJS,在服务器端,我使用ASP。净之前。

I have two view models, ProductCriteriaViewModel and SimpleDisplayFieldViewModel:

我有两个视图模型,ProductCriteriaViewModel和SimpleDisplayFieldViewModel:

public class ProductCriteriaViewModel
{
    public int ID { get; set; }
    public int? UserSearchID { get; set; }
    public bool? Enabled { get; set; }
    public SimpleDisplayFieldViewModel Property { get; set; }
    public string Operator { get; set; }
    public string CriteriaValue { get; set; }
}

public class SimpleDisplayFieldViewModel
{
    public string Name { get; set; }
    public string Value { get; set; }
    public string PropertyType { get; set; }
}

In Angular, I submit a POST request to a WebAPI controller action with the following signature:

在角度上,我向WebAPI控制器动作提交一个POST请求,签名如下:

    public IList<...> FindProducts(List<ProductCriteriaViewModel> criteriaVM, bool userFiltering)
    {
       ...
    }

In testing, I tried to send an array of Product Criterias, and checked Fiddler to see what the array looked like in the body of the POST request when it was being sent to the server. This is what the array looked like:

在测试中,我尝试发送一个产品标准数组,并检查Fiddler,以查看POST请求发送到服务器时该数组在请求体中的外观。这个数组是这样的:

[
{"Enabled":true,
 "Operator":"Less than",
 "Property":
     {"$id":"2",                
      "Name":"Copyright Year",
      "Value":"Basic",
      "PropertyType":null},
 "CriteriaValue":"2013",
 "IsNew":true},

{"Enabled":true,
"Operator":"Greater Than",
"Property":
    {"$id":"2",               
     "Name":"Copyright Year",
     "Value":"Basic",
     "PropertyType":null},
"CriteriaValue":"1988",
"IsNew":true}
]

The above array has the correct values, however the result of deserialization on the server-side is incorrect. This is where it gets strange.

上面的数组具有正确的值,但是服务器端反序列化的结果是不正确的。这就是它变得奇怪的地方。

After the server deserializes the array and arrives in the controller action, the first element in criteriaVM is correct, all the values are set properly. However the second element is incorrect, CriteriaValue and Property are nulled out:

在服务器反序列化数组并到达控制器操作之后,criteriaVM中的第一个元素是正确的,所有值都被正确设置。然而,第二个要素是不正确的,标准和财产被无效:

WebAPI -在服务器端不正确反序列化的对象数组。

This issue only occurs whenever I choose the same search property for more than one criteria (i.e. Copyright < 2013 and Copyright > 1988). However, if I choose different properties (i.e. Copyright < 2013 and Price > 20), then all elements in the resulting criteriaVM are correctly initialized.

只有当我为多个条件选择相同的搜索属性(即Copyright < 2013和Copyright > 1988)时,才会出现此问题。但是,如果我选择了不同的属性(比如版权< 2013和Price > 20),那么结果标准中的所有元素都被正确初始化了。

I do not understand what could be causing this issue. Why are only CriteriaValue and Property set to null in the second element of the List? Why does this issue only occur when I choose multiples of the same search properties?

我不明白是什么原因导致了这个问题。为什么在列表的第二个元素中仅将CriteriaValue和属性设置为null ?为什么这个问题只发生在我选择相同搜索属性的倍数时?

1 个解决方案

#1


2  

Json.NET uses the keywords $id and $ref in order to preserve object references, so you are having troubles with your deserialization because your JSON has "$id" in the "Property" object. See this link for more information about object references.

Json。NET使用$id和$ref来保存对象引用,所以您的反序列化遇到了麻烦,因为您的JSON在“属性”对象中有“$id”。有关对象引用的更多信息,请参见此链接。

In order to fix your deserialization issues, you can add the following line in the Register method of your WebApiConfig.cs class

为了解决反序列化问题,您可以在WebApiConfig的Register方法中添加以下一行。计算机科学类

config.Formatters.JsonFormatter.SerializerSettings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;

If your Web Api project does not include a WebApiConfig.cs class, simply add the configuration in your Global.asax:

如果您的WebApi项目不包含WebApiConfig。cs类,只需在您的Global.asax中添加配置即可。

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;

Now your object in the web api method should look like this:

现在,web api方法中的对象应该如下所示:

WebAPI -在服务器端不正确反序列化的对象数组。

#1


2  

Json.NET uses the keywords $id and $ref in order to preserve object references, so you are having troubles with your deserialization because your JSON has "$id" in the "Property" object. See this link for more information about object references.

Json。NET使用$id和$ref来保存对象引用,所以您的反序列化遇到了麻烦,因为您的JSON在“属性”对象中有“$id”。有关对象引用的更多信息,请参见此链接。

In order to fix your deserialization issues, you can add the following line in the Register method of your WebApiConfig.cs class

为了解决反序列化问题,您可以在WebApiConfig的Register方法中添加以下一行。计算机科学类

config.Formatters.JsonFormatter.SerializerSettings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;

If your Web Api project does not include a WebApiConfig.cs class, simply add the configuration in your Global.asax:

如果您的WebApi项目不包含WebApiConfig。cs类,只需在您的Global.asax中添加配置即可。

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;

Now your object in the web api method should look like this:

现在,web api方法中的对象应该如下所示:

WebAPI -在服务器端不正确反序列化的对象数组。