【问题标题】:How to Post JSON data to a Web API using HttpClient如何使用 HttpClient 将 JSON 数据发布到 Web API
【发布时间】:2016-07-06 10:24:40
【问题描述】:

我有以下代码,基本上它接受一个动态对象(在这种情况下是类型文件)并使用HTTPClient 类尝试向WebAPI controller POST,我遇到的问题是控制器我的 [FromBody] 参数上的值总是得到 NULL

代码

var obj = new
        {
            f = new File
            {
                Description = description,
                File64 = Convert.ToBase64String(fileContent),
                FileName = fileName,
                VersionName = versionName,
                MimeType = mimeType
            },
        }

var client = new HttpClient(signingHandler)
{
   BaseAddress = new Uri(baseURL + path) //In this case v1/document/checkin/12345
};

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

HttpResponseMessage response;
action = Uri.EscapeUriString(action);

//Obj is passed into this, currently it is of type File 
var content = new StringContent(JsonConvert.SerializeObject(obj).ToString(),
            Encoding.UTF8, "application/json");

response = client.PostAsync(action, content)).Result;
if (response.IsSuccessStatusCode)
{     
    var responseContent = response.Content;                
    string responseString = responseContent.ReadAsStringAsync().Result;
    return JsonConvert.DeserializeObject<T>(responseString);
}

控制器

[HttpPost]
[Route("v1/document/checkin/{id:int}")]
public void Checkin_V1(int id, [FromBody] File f)
{
        //DO STUFF - f has null on all of its properties
}

型号

public class File
{
    public string FileName { get; set; }
    public string VersionName { get; set; }
    public string Description { get; set; }
    public string MimeType { get; set; }
    public byte[] Bytes { get; set;}
    public string File64 { get; set; }
}

模型在WebAPI 和客户端应用程序上共享。

任何关于为什么失败的帮助将不胜感激,现在已经绕了一段时间了。

【问题讨论】:

    标签: c# json asp.net-web-api httpclient


    【解决方案1】:

    不需要你一开始的 obj。那就是将 f 嵌套在另一个对象中。

    var obj = new
        {
            f = new File
            {
                Description = description,
                File64 = Convert.ToBase64String(fileContent),
                FileName = fileName,
                VersionName = versionName,
                MimeType = mimeType
            },
        }
    

    改成

    var f = new File
    {
        Description = description,
        File64 = Convert.ToBase64String(fileContent),
        FileName = fileName,
        VersionName = versionName,
        MimeType = mimeType
    };
    

    然后只需序列化 f。

    【讨论】:

      【解决方案2】:

      我认为你的这部分代码有问题

          var obj = new
          {
              f = new File
              {
                  Description = description,
                  File64 = Convert.ToBase64String(fileContent),
                  FileName = fileName,
                  VersionName = versionName,
                  MimeType = mimeType
              },
          }
      

      因为这将与您真正需要的序列化不同。 试试这个

         var obj =  new File
              {
                  Description = description,
                  File64 = Convert.ToBase64String(fileContent),
                  FileName = fileName,
                  VersionName = versionName,
                  MimeType = mimeType
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多