ASP.NET MVC Read Raw JSON Post Data

后端 未结 3 2034
醉话见心
醉话见心 2020-12-08 19:30

This is driving me crazy. I\'m using ASP.NET MVC. I have a controller with an HttpPost action that acts as a callback URL that is called by another server (not under my cont

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-08 20:17

    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();
    
        // ...
    }
    

提交回复
热议问题