如何从ApiController中的StringContent对象读取JSON? [英] How can I read JSON from a StringContent object in an ApiController?

查看:153
本文介绍了如何从ApiController中的StringContent对象读取JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个API控制器,旨在接收和解析JSON异步帖子的内容,并且无法读取该帖子中StringContent对象的内容.

I'm writing an API controller intended to receive and parse the contents of a JSON asynchronous post, and am unable to read the contents of the StringContent object in that post.

这是我希望在API控制器中看到此值的部分.到达ApiController方法的值是null. jsonContent值是一个空字符串.我期望看到的是JSON对象的内容.

Here is the section from my API controller where I expect to see the value. The value arriving in the ApiController method is null. And the jsonContent value is an empty string. What I'm expecting to see is the contents of a JSON object.

public class ValuesController : ApiController
{
    // POST api/values
    public void Post([FromBody]string value)
    {
        HttpContent requestContent = Request.Content;
        string jsonContent = requestContent.ReadAsStringAsync().Result;

        // Also tried this per mybirthname's suggestion.
        // But content ends up equaling 0 after this runs.
        var content = Request.Content.ReadAsStreamAsync().Result.Seek(0, System.IO.SeekOrigin.Begin);
    }
}

这是我的控制器,用于显示其调用方式.

here is my controller to show how it's being called.

[HttpPost]
public ActionResult ClientJsonPoster(MyComplexObject myObject)
{
    this.ResponseInfo = new ResponseInfoModel();
    PostToAPI(myObject, "http://localhost:60146", "api/values").Wait();
    return View(this.ResponseInfo);
}

这是发布方法.

private async Task PostToAPI(object myObject, string endpointUri, string endpointDirectory)
{
    string myObjectAsJSON = System.Web.Helpers.Json.Encode(myObject);
    StringContent stringContent = new StringContent(myObjectAsJSON, Encoding.UTF8, "application/json");
    using (HttpClient httpClient = new HttpClient())
    {
        httpClient.BaseAddress = new Uri(endpointUri);
        using (HttpResponseMessage responseMessage = await httpClient.PostAsJsonAsync(endpointDirectory, stringContent).ConfigureAwait(false))
        {
            // Do something
        }
    }
}

我怀疑ApiController内部的Post方法的签名有问题.但是不知道该如何更改.感谢您的帮助.

I suspect something is wrong with the signature of the Post method inside the ApiController. But don't know how that should be changed. Thanks for your help.

推荐答案

您正在混合异步和同步调用,这将导致死锁.

You are mixing async and sync calls which will lead to deadlocks.

将控制器更新为

[HttpPost]
public async Task<ActionResult> ClientJsonPoster(MyComplexObject myObject) {
    this.ResponseInfo = new ResponseInfoModel();
    await PostToAPI(myObject, "http://localhost:60146", "api/values");
    return View(this.ResponseInfo);
}

[FromBody] 用于强制Web API从请求正文中读取简单类型.

Also [FromBody] is used to force Web API to read a simple type from the request body.

更新Api

public class ValuesController : ApiController {
    // POST api/values
    [HttpPost]
    public async Task Post() {
        var requestContent = Request.Content;
        var jsonContent = await requestContent.ReadAsStringAsync();

    }
}

这篇关于如何从ApiController中的StringContent对象读取JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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