ASP.NET MVC Read Raw JSON Post Data

不羁的心 提交于 2019-11-28 06:47:41

Your initial approach should work if you take into consideration the fact, that ASP.NET MVC model binding has already read the stream, so you should rewind it:

[HttpPost]
public ActionResult Callback(string secret)
{
    Request.InputStream.Seek(0, SeekOrigin.Begin);
    string jsonData = new StreamReader(Request.InputStream).ReadToEnd();

    // ...
}
Abhishek7997

Reset the position to Zero before reading the stream. Request.InputStream.Position = 0

For ASP.NET Core 2,this works for me.

    [HttpPost]
    public ActionResult RawTest() {
        using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
        {  
            string content = reader.ReadToEndAsync().Result;
            //...
        }
        //...
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!