【问题标题】:Create WebAPI post from Console to include $type in json data从控制台创建 WebAPI 帖子以在 json 数据中包含 $type
【发布时间】:2014-03-05 20:39:22
【问题描述】:

我正在创建对象并将它们发布到 webapi。基本上我只是无法让这些该死的东西序列化,以便在 json 中包含 $type 信息。以下是我正在尝试编写的代码。之后是我期望的json。

       var cds = new List<CreditDefaultSwaps>()
        {
            new CreditDefaultSwaps() { ModelNumber = "SP8A1ETA", BrokerSpread = 0},
            new CreditDefaultSwaps() { ModelNumber = "SP3A0TU1", BrokerSpread = 0},
            new CreditDefaultSwaps() { ModelNumber = "SP4A102V", BrokerSpread = 0}
        };

        var client = new HttpClient {BaseAddress = new Uri("http://localhost/BloombergWebAPI/api/")};

        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        // set up request object
        var oContract = new WebApiDataServiceRequest
        {
            RequestType = ReferenceDataRequestServiceTypes.ReferenceDataRequest,
            SwapType = BloombergWebAPIMarshal.SwapType.CDS,
            SecurityList = cds
        };

        Tried something like this and the var content was formatted as I would expect
        however I couldn't post the data using postasjsonasync

        //var content = JsonConvert.SerializeObject(oContract, Formatting.Indented,
        //    new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects });

           Console.ReadLine();

        var response = client.PostAsJsonAsync("bloombergapi/processbloombergrequest", oContract).Result;

以下是我要发布的 json。我在上面的代码中遗漏了什么,我确定这很愚蠢。

   {
      "$type": "BloombergWebAPIMarshal.WebApiDataServiceRequest, BloombergWebAPIMarshal",
      "RequestType": 3,
      "SwapType": 1,
      "SecurityList": [
        {
          "$type": "BloombergWebAPIMarshal.CreditDefaultSwaps, BloombergWebAPIMarshal",
          "ModelNumber": "SP8A1ETA",
          "BrokerSpread": 0
        },
        {
          "$type": "BloombergWebAPIMarshal.CreditDefaultSwaps, BloombergWebAPIMarshal",
          "ModelNumber": "SP3A0TU1",
          "BrokerSpread": 0
        },
        {
          "$type": "BloombergWebAPIMarshal.CreditDefaultSwaps, BloombergWebAPIMarshal",
          "ModelNumber": "SP4A102V",
          "BrokerSpread": 0
        }
      ]
    }

【问题讨论】:

    标签: asp.net-mvc-4 asp.net-web-api


    【解决方案1】:

    创建了另一个重载使用此调用来产生正确的请求:

    var response = client.PostAsJsonAsync("processbloombergrequest", oContract, TypeNameHandling.Objects).Result
    

    这是新的重载

    public static Task<HttpResponseMessage> PostAsJsonAsync<T>(this HttpClient client, string requestUri, T value, TypeNameHandling typeNameHandling)
    {
    
        return client.PostAsJsonAsync<T>(requestUri, value, CancellationToken.None, typeNameHandling);
    }
    
    public static Task<HttpResponseMessage> PostAsJsonAsync<T>(this HttpClient client, string requestUri, T value, CancellationToken cancellationToken, TypeNameHandling typeNameHandling)
    {
        var formatter = new JsonMediaTypeFormatter
        {
            SerializerSettings = new JsonSerializerSettings()
            {
                TypeNameHandling = typeNameHandling
            }
        };
    
        return client.PostAsync<T>(requestUri, value, formatter, cancellationToken);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      • 1970-01-01
      • 2011-04-13
      • 2015-07-29
      • 2011-11-20
      • 1970-01-01
      相关资源
      最近更新 更多