Returning 'Allow' Entity Header with HTTPResponseMessage

Topics: Web Api
Jan 2, 2012 at 1:06 PM

Hi,

I'm trying to return the 'Allow' entity header within the response message and I keep getting the following message:

{"The header cannot be added. Make sure to add request headers to HttpRequestMessage, response headers to HttpResponseMessage, and content headers to HttpContent objects."}

Here's the code snippet:

[WebInvoke(UriTemplate = "{id}", Method = "DELETE")]
        public HttpResponseMessage<Order> DeleteOrder(int id)
        {
            HttpResponseMessage<Order> response = null;

            try
            {
                if (id <= 0)
                {
                    response = new HttpResponseMessage<Order>(HttpStatusCode.BadRequest);
                }
                else
                {
             
                    if (id == 12345)
                    {                      
                        return new HttpResponseMessage<Order>(HttpStatusCode.NoContent);
                    }

                    if (id == 123456)
                    {                       
                        response = new HttpResponseMessage<Order>(HttpStatusCode.MethodNotAllowed);
                        response.Headers.AddWithoutValidation("Allow", "PUT");
                    }

                    // return '404 - Not Found' status code
                    response = new HttpResponseMessage<Order>(HttpStatusCode.NotFound);
                   
                }

                return response;
            }
            catch (Exception ex)
            {
                return response = new HttpResponseMessage<Order>(HttpStatusCode.InternalServerError);
            }
        }

Any advice would be very helpful.

Thanks,

dorman

Jan 3, 2012 at 7:02 AM

Try using:

response.Content.Headers.AddWithoutValidation("Allow", "PUT");

instead....

Jan 5, 2012 at 12:17 AM

Thanks smolesen, that worked! How come there is no reference about that in the documentation?