通过PostAsJsonAsync HTTP POST多个对象 [英] HTTP Post multiple objects through PostAsJsonAsync

查看:1020
本文介绍了通过PostAsJsonAsync HTTP POST多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的HttpClient一些的WebAPI。

I'm using HttpClient with some WebAPI.

我需要多个对象发送到POST,这是我Post方法的声明方式:

I need to send multiple objects to a POST, here's how my Post method is declared:

public string Post(Models.Client value, App.ControlCenter.Models.Company c) 
{
    ...
}

这就是我如何打电话到的WebAPI:

And here's how I'm calling to the WebAPI:

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("http://localhost/");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var s = client
       .PostAsJsonAsync("api/Client/", c)
       .Result
       .Content.ReadAsAsync<App.ControlCenter.Models.RolDTO>().Result;
    return View();
}

我需要做的是同时发送客户对象和公司对我的帖子的方法来工作。

What I need to do is send both the Client object and the Company for my Post method to work.

推荐答案

您需要创建一个DTO类类型的两个属性的 Models.Client App.ControlCenter.Models.Company

Client Side

You need to create a DTO class with two properties of types Models.Client and App.ControlCenter.Models.Company

public class DTO.ComplexObject  
{     
  public Models.Client tClientModel { get; set; }

  public App.ControlCenter.Models.Company tCompany{ get; set; }
}

然后填写ComplexObject对象(即TComplexObject),并使用

and then fill ComplexObject object (i.e., TComplexObject) and use

HttpResponseMessage tResponse = tHttpClient.PostAsJsonAsync(url,TComplexObject).Result;

API

[HttpPost]
public HttpResponseMessage AddData(DTO.ComplexObject tComplexObject)
{

这篇关于通过PostAsJsonAsync HTTP POST多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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