没有[FromBody]的HttpClient发布到API

时间:2018-03-29 15:02:34

标签: c# asp.net-core

我希望在没有FromBody标签的情况下将复杂对象发送到我的.NET Core API。

我希望用JQuery做的很简单,但我无法弄清楚如何从C#Web API客户端复制逻辑。

为了测试,我有一个相当简单的对象。

[Serializable]
public class FilterModel
{
    public int? PageSize { get; set; }
    public int Page { get; set; }
}

为了测试,我们还有一个非常简单的控制器方法

[HttpPost("TEST")]
public virtual int GetTEST(Common.FilterModel filter, Common.FilterModel filter2)
{
    return ((filter.Page * filter.PageSize ?? 0) + (filter2.Page * filter2.PageSize ?? 0));
}

显然代码的用例会让我更复杂,但我只是想在这个时候获取值。

调用此方法的JQuery非常简单,并且完美无缺:

var myData = {"filter":{"pageSize":3,"page":2},
            "filter2":{"pageSize":19,"page":1}};
$.ajax({
  type: 'POST',
  url: '/api/Authentication/TEST',
  data: myData
}).done(function (data, statusText, xhdr) {
  JsonResponse = data;
  console.log(data);
}).fail(function (xhdr, statusText, errorText) {
  //console.log(JSON.stringify(xhdr));
});

但是尝试从HttpClient复制该逻辑会导致0;在API端接收时,模型始终为空。我已经尝试了一些不同的方法,从JSON序列化ArrayList / Dictionary到我在下面添加的内容,但我不知道我哪里出错了。

        HttpClient client = new HttpClient
        {
            BaseAddress = new Uri("http://localhost:65447/")
        };
        ApiCommon.FilterModel filter = new ApiCommon.FilterModel()
        {
            PageSize = 10,
            Page = 1
        };
        ApiCommon.FilterModel filter2 = new ApiCommon.FilterModel()
        {
            PageSize = 9,
            Page = 41
        };


        MultipartFormDataContent mpContent = new MultipartFormDataContent();
        StringContent content = new StringContent(JsonConvert.SerializeObject(filter));
        content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
        mpContent.Add(content, "filter");
        content = new StringContent(JsonConvert.SerializeObject(filter2));
        content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
        mpContent.Add(content, "filter2");
        var retval = 
              JsonConvert.DeserializeObject<int>(
                  client.PostAsync("/api/Authentication/TEST", mpContent)
                  .Result.Content.ReadAsStringAsync().Result);
        Console.WriteLine(retval);

有没有办法模仿JQuery调用来通过HttpClient工作?我错过了一些关键的东西吗?

编辑: 在让小提琴手拿起它之后,这是每个的原始数据,我将尝试将其与我的更改相匹配。

JQuery fiddler

ePOST http://localhost:65447/api/Authentication/TEST HTTP/1.1
Host: localhost:65447
Connection: keep-alive
Content-Length: 86
Accept: */*
Origin: http://localhost:65447
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:65447/TestHarness
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
filter%5BpageSize%5D=3&filter%5Bpage%5D=2&filter2%5BpageSize%5D=19&filter2%5Bpage%5D=1

C#fiddler

POST http://local.dev:65447/api/Authentication/TEST HTTP/1.1
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Host: local.dev:65447

156
{"filter":{"<IncludeFullData>k__BackingField":false,"<SortBy>k__BackingField":"","<PageSize>k__BackingField":10,"<Page>k__BackingField":1,"<Operation>k__BackingField":""},"filter2":{"<IncludeFullData>k__BackingField":false,"<SortBy>k__BackingField":"","<PageSize>k__BackingField":9,"<Page>k__BackingField":41,"<Operation>k__BackingField":""}}
0

0 个答案:

没有答案