Read HttpContent in WebApi controller

前端 未结 3 686
执念已碎
执念已碎 2020-12-02 11:49

How can I read the contents on the PUT request in MVC webApi controller action.

[HttpPut]
public HttpResponseMessage Put(int accountId, Contact contact)
{
           


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-02 12:27

    You can keep your CONTACT parameter with the following approach:

    using (var stream = new MemoryStream())
    {
        var context = (HttpContextBase)Request.Properties["MS_HttpContext"];
        context.Request.InputStream.Seek(0, SeekOrigin.Begin);
        context.Request.InputStream.CopyTo(stream);
        string requestBody = Encoding.UTF8.GetString(stream.ToArray());
    }
    

    Returned for me the json representation of my parameter object, so I could use it for exception handling and logging.

    Found as accepted answer here

提交回复
热议问题