Question

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
                {
                    // For brevity, I'm assuming that order - 123456 was already served and logged. Hence it cannot
                    // be deleted. Order 12345, however, can be deleted.
                    // Note: The code doesn't actual delete anything. This is just a demonstration of
                    // the DELETE verb
                    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

Was it helpful?

Solution

Try response.Content.Headers instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top