【问题标题】:Asp.net Mvc receives a complex object as a list of key/value pair arrayAsp.net Mvc 接收一个复杂对象作为键/值对数组的列表
【发布时间】:2017-08-14 12:05:21
【问题描述】:

我有两个 asp.net mvc 应用程序试图通过将一个复杂的序列化对象从一个应用程序发送到另一个应用程序来相互通信。

但是当数据到达另一端时,对象作为键值对数组的列表进来。以下是尝试发送/接收内容的详细信息。

这是用于发送 json 对象的代码

 using (var hc = new HttpClient())
            {
                hc.DefaultRequestHeaders.Accept.Clear();
                hc.DefaultRequestHeaders.Accept.Add(Media);

                var dict = new Dictionary<string, DejaVuObject> {{"Entity", obj}};

                var strinified = JsonConvert.SerializeObject(dict);
                var stringContent = new StringContent(strinified, Encoding.UTF8, "application/json");

                var response =  await hc.PostAsync(url, stringContent);
                return response;
            }

接收方法签名

[HttpPost]
public ActionResult RecieveEntity(Dictionary<string, object> post)

这是发送的内容

{
 "Main Service ID": "1",
      "Node ID": "",
      "Parameters": [
         {
            "Name": "firstname",
            "Validation": {
               "Entity": 0,
               "EntityListFilter": "",
               "IsNotEditable": false,
               "IsPrimaryIdentifier": false,
               "IsRequired": true,
               "IsUnique": false,
               "Parameter Format": 0,
               "ParameterMode": ""
            }
         }
      ],
      "CustomEvents": [
         {
            "Description": "event description",
            "Message": "new message",
            "MilestoneCondition": "milestone information.",
            "Name": "new message",
            "TheFields": []
         }
      ],
      "Processings String": "Action failed.[TN01-31:Action failed]"
}

这是收到的内容

{
 "Processings String": "Action failed.[TC01-71:Action failed while processing the request.]::Action succeeded.[TC01-54:Processing this command was successful.]",
   "Parameters": "[Name, Firstname][Validation, ][Key, 2e431711-2ba9-40ef-985e-dbfa8c13a932][isrequired, True][fieldname, ][Name, Lastname][Validation, ][Key, be4de2d6-d39e-44fa-8f31-b4b0964f82da]",
   "CustomEvents": "[Description, Processing this command was successful][Message, Action suceeded][MilestoneCondition, When command processing suceeds.][Name, Action suceeded][TheFields, System.Collections.Generic.List`1[System.Dynamic.DejaVuObject]]",
   "Main Service ID": "1"
}

如果你仔细查看其他系统的内容,你会发现大多数内部对象是一个键/值对数组,而不是发送的普通对象数组。我做错了什么,我该如何纠正它?

【问题讨论】:

  • 接收方法签名是什么?
  • @krishna 查看我的编辑。
  • 在两端将字典更改为动态,如Dictionary&lt;string,dynamic&gt;
  • 另一种选择是在发送和接收应用程序中定义一个视图模型,并让模型绑定发挥它的魔力。 (当然,除非您需要端点比这更具动态性......)
  • @Krishna 问题是我不能真正改变第二个应用程序的定义,因为它已经被其他系统使用而不会破坏某些东西,我只能做的是弄清楚如何制作第一个应用程序发送其他应用程序可以正确反序列化的内容,因为它已经在其他系统中运行。

标签: c# json asp.net-mvc json.net


【解决方案1】:

这是您使用字典类型来接收内容的事实。根据您的定义,此时数据包是从 app1 发送的。所以更新 app1 对你没有帮助。

@Krishna 建议更新签名以使用动态,或者您可以扩展您当前的 app2 方法以处理新界面。

您有来自现有集成应用程序之一的数据包示例吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    相关资源
    最近更新 更多