Void method call results for PUT and POST

Topics: ASP.NET Web API
Apr 12, 2012 at 10:29 PM

When you have a void action method:

public void Put(Order payload)

or 

public void Post(Order payload)

By specifying a void return, shouldn't the default status code be 204? Or any action method for that matter. Right now it's 200, with empty content.

Thoughts?

Apr 12, 2012 at 10:40 PM

+1. OData does 204, for void Actions.

Apr 12, 2012 at 11:33 PM

This sounds like a good idea. Methods returning void or Task could easily have a status code of 204 instead of 200. This would be a 1-line change in the source.

One thing to note here is that filters and message handlers need to be aware of this behavior and do the right thing to avoid having a response with content and a 204 status. But that's general guidance the applies not just to this situation.

Apr 13, 2012 at 3:36 PM

Pull request here:

http://aspnetwebstack.codeplex.com/SourceControl/network/forks/jbogard/master/contribution/2076

The NoContentOnNoReturn branch.

Jun 1, 2012 at 3:31 AM
Edited Jun 1, 2012 at 3:31 AM

XmlHttpRequest in internet explorer throws an "operation aborted" exception if you return 204.

http://stackoverflow.com/questions/4268931/xmlhttprequest-response-has-no-headers-in-internet-explorer

Worked around in jquery, but considering this bug exists in internet explorer, it may not be worth returning the correct status code if it causes other issues down the road. The workaround jquery uses drops all response headers