If something goes wrong in a WCF REST call, such as the requested resource is not found, how can I play with the HTTP response code (setting it to something like HTTP 404, for example) in my OperationContract method?
7 Answers
There is a WebOperationContext
that you can access and it has a OutgoingResponse
property of type OutgoingWebResponseContext
which has a StatusCode
property that can be set.
WebOperationContext ctx = WebOperationContext.Current;
ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;

- 47,184
- 49
- 157
- 202
-
5Does this work inside WCF Data Services - Service Operations ? I haven't had luck, seems the StatusCode I set gets trumped by something else. So on all HTTP POST requests, I get back 204 regardless that I set it at 201,etc. – RyBolt Dec 22 '10 at 15:32
-
1Doesn't work in my case, the status gets overwritten. Throwing a `WebFaultException`, however, does seem to work. – Josh M. Feb 04 '14 at 23:21
If you need to return a reason body then have a look at WebFaultException
For example
throw new WebFaultException<string>("Bar wasn't Foo'd", HttpStatusCode.BadRequest );

- 3,693
- 22
- 29
-
4I like this better than the accepted one since we are not using the static WebOperationContext.Current – Zephyr was a Friend of Mine Dec 06 '11 at 20:43
-
keep in mind this is only valid since famework 4 http://msdn.microsoft.com/en-us/library/dd989924.aspx – sebagomez May 16 '12 at 18:10
-
1hmm, in .NET 4.5.1 this is not setting the status code for me, I'm still getting a 200. I'm using jsonp, it's calling my callback (in javascript) and passing my message and my status code as an integer. – Shavais Mar 19 '14 at 00:29
-
7This seems great for anything outside of the 2XX codes, but would you throw a `WebFaultException` to return an `HttpStatusCode.Created`? – crush Oct 09 '14 at 15:34
For 404 there is a built in method on the WebOperationContext.Current.OutgoingResponse called SetStatusAsNotFound(string message) that will set the status code to 404 and a status description with one call.
Note there is also, SetStatusAsCreated(Uri location) that will set the status code to 201 and location header with one call.

- 18,845
- 14
- 46
- 43
You can also return a statuscode and reason body with WebOperationContext's StatusCode and StatusDescription:
WebOperationContext context = WebOperationContext.Current;
context.OutgoingResponse.StatusCode = HttpStatusCode.OK;
context.OutgoingResponse.StatusDescription = "Your Message";

- 93
- 2
- 8
If you wish to see the status description in the header, REST method should make sure to return null from the Catch() section as below:
catch (ArgumentException ex)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.InternalServerError;
WebOperationContext.Current.OutgoingResponse.StatusDescription = ex.Message;
return null;
}
-
1This didn't work for me either, I still get a 200. I'm using WebHttpBinding with crossDomainScriptAccessEnabled="true" and an end point behaviour of webHttp with a default body style of wrapped and a default outgoing response format of json.. but that shouldn't matter, should it? – Shavais Mar 19 '14 at 00:32
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Unauthorized;
throw new WebException("令牌码不正确", new InvalidTokenException());

- 11
- 1
This did not work for me for WCF Data Services. Instead, you can use DataServiceException in case of Data Services. Found the following post useful. http://social.msdn.microsoft.com/Forums/en/adodotnetdataservices/thread/f0cbab98-fcd7-4248-af81-5f74b019d8de

- 353
- 6
- 18
-
That doesn't work for me either, I just get a 400 Bad Request, with no other useful information, and no response body. – Shavais Mar 19 '14 at 00:44