Get json in request body with ASP.NET MVC

Viewed 442

I want to get json from body of request.

This code is working :

public ActionResult SaveConfiguration()
{
    var input = new StreamReader(HttpContext.Request.InputStream).ReadToEnd();

}

But if I add a binding (from uri) in my action, that not works anymore...

public ActionResult SaveConfiguration(int id)
{
    var input = new StreamReader(HttpContext.Request.InputStream).ReadToEnd();

}

I am using ASP.NET MVC (not ASP.NET Core)

1 Answers

Finally found the solution (reset position on stream) :

public ActionResult SaveConfiguration(int id)
        {
            Request.InputStream.Position = 0;  // <= Solution
            var input = new StreamReader(HttpContext.Request.InputStream).ReadToEnd();
Related