I have a Post method that has the following signature:
public HttpResponseMessage<SubmitFeedResponse> Post(AmazonEnvelope envelope)
In the body I would like to return an indication of an error if one occurs. So I have the following code:
HttpResponseMessage errorResponse = this.ControllerContext.Request.CreateResponse();
errorResponse.Content = new StringContent("No connection string");
return errorResponse as HttpResponseMessage<SubmitFeedResponse>;
But this only returns an empty SubmitFeedResponse object with no error and the string content is not set. In such a case how do I return an error? I could also just new an HttpResponseMessage<SubmitFeedResponse> but then I get a whole buch of errors basically
telling me I need to use CreateContent<SubmitFeedResponse> to create and ObjectContent<SubmitFeedResponse> but then I don't know where to put my error message (StringContent).
You could throw HttpResponseException with the message in your controller. The messages thrown as part of these exceptions are sent back to the client in a content negotiated way.
Thanks,
Kiran Challa
Marked as answer by Kevin Burton on Mar 21, 2012 01:18 AM
Kevin Burton
Member
79 Points
66 Posts
Error response in a controller.
Mar 20, 2012 09:09 PM|LINK
I have a Post method that has the following signature:
In the body I would like to return an indication of an error if one occurs. So I have the following code:
HttpResponseMessage errorResponse = this.ControllerContext.Request.CreateResponse(); errorResponse.Content = new StringContent("No connection string"); return errorResponse as HttpResponseMessage<SubmitFeedResponse>;But this only returns an empty SubmitFeedResponse object with no error and the string content is not set. In such a case how do I return an error? I could also just new an HttpResponseMessage<SubmitFeedResponse> but then I get a whole buch of errors basically telling me I need to use CreateContent<SubmitFeedResponse> to create and ObjectContent<SubmitFeedResponse> but then I don't know where to put my error message (StringContent).
Thank you.
rkevinburton@charter.net
kevin_burton@baxter.com
Kiran Challa
Participant
1460 Points
285 Posts
Microsoft
Re: Error response in a controller.
Mar 21, 2012 12:39 AM|LINK
You could throw HttpResponseException with the message in your controller. The messages thrown as part of these exceptions are sent back to the client in a content negotiated way.
Kiran Challa