89

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

[HttpPut]
public HttpResponseMessage Put(int accountId, Contact contact)
{
    var httpContent = Request.Content;
    var asyncContent = httpContent.ReadAsStringAsync().Result;
...

I get empty string here :(

What I need to do is: figure out "what properties" were modified/sent in the initial request (meaning that if the Contact object has 10 properties, and I want to update only 2 of them, I send and object with only two properties, something like this:

{

    "FirstName": null,
    "LastName": null,
    "id": 21
}

The expected end result is

List<string> modified_properties = {"FirstName", "LastName"}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Marty
  • 3,485
  • 8
  • 38
  • 69

3 Answers3

164

By design the body content in ASP.NET Web API is treated as forward-only stream that can be read only once.

The first read in your case is being done when Web API is binding your model, after that the Request.Content will not return anything.

You can remove the contact from your action parameters, get the content and deserialize it manually into object (for example with Json.NET):

[HttpPut]
public HttpResponseMessage Put(int accountId)
{
    HttpContent requestContent = Request.Content;
    string jsonContent = requestContent.ReadAsStringAsync().Result;
    CONTACT contact = JsonConvert.DeserializeObject<CONTACT>(jsonContent);
    ...
}

That should do the trick (assuming that accountId is URL parameter so it will not be treated as content read).

tpeczek
  • 23,867
  • 3
  • 74
  • 77
  • Thanx. And yes, account id is a url param. – Marty Sep 19 '12 at 12:51
  • 1
    I wonder if you could create a MessageHandler that called LoadIntoBuffer() on the request content before the Model binder kicked in. – Darrel Miller Sep 19 '12 at 14:37
  • @DarrelMiller I'm not sure if ASP.NET Web API would still bind the model - needs to be tested. – tpeczek Sep 19 '12 at 19:56
  • Will this cause Deadlocking? – Airn5475 Oct 16 '18 at 21:19
  • 3
    @Airn5475 As it is, it may. You should switch action signature to async and use await. – tpeczek Oct 16 '18 at 21:46
  • @tpeczek why can it only be read once? Just wondering if there was any reason for this when they designed it. – David Klempfner Sep 11 '19 at 12:27
  • @Backwards_Dave It's a network stream. So unless you buffer it, it's a read once, forward only stream due to actual source of data. – tpeczek Oct 26 '19 at 17:44
  • I'm using .NET 5.0 and in my method, my Request object (MicrosoftAspNetCore.Http.DefaultHttpRequest) I have a ContentLength and ContentType (both with correct values) but I don't see a Content property? Am I missing something? I'm trying to figure out how to read the file I'm posting to the API. It's of type application/x-msdownload. – Christopher Painter Jul 21 '21 at 15:01
  • @ChristopherPainter This answer is specific to ASP.NET Web API (old good .NET Framework one). A lot has changed in ASP.NET Core. – tpeczek Jul 27 '21 at 19:17
21

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

Community
  • 1
  • 1
Anytoe
  • 1,605
  • 1
  • 20
  • 26
5

Even though this solution might seem obvious, I just wanted to post it here so the next guy will google it faster.

If you still want to have the model as a parameter in the method, you can create a DelegatingHandler to buffer the content.

internal sealed class BufferizingHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        await request.Content.LoadIntoBufferAsync();
        var result = await base.SendAsync(request, cancellationToken);
        return result;
    }
}

And add it to the global message handlers:

configuration.MessageHandlers.Add(new BufferizingHandler());

This solution is based on the answer by Darrel Miller.

This way all the requests will be buffered.

Community
  • 1
  • 1
derwasp
  • 802
  • 8
  • 17