91

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?

kgriffs
  • 4,080
  • 5
  • 37
  • 42
  • ok all the answers to this assume control made it into your service implementation. what if they pass some totally invalid uri? how are you suppose to provide a 404 for all the unexpected hits to your service? – Nathan Tregillus Jul 18 '12 at 22:56

7 Answers7

120

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;
Eric Schoonover
  • 47,184
  • 49
  • 157
  • 202
  • 5
    Does 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
  • 1
    Doesn'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
78

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 );
Graeme Bradbury
  • 3,693
  • 22
  • 29
  • 4
    I 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
  • 1
    hmm, 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
  • 7
    This 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
25

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.

JarrettV
  • 18,845
  • 14
  • 46
  • 43
6

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";
eitzo
  • 93
  • 2
  • 8
3

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;
}
takrl
  • 6,356
  • 3
  • 60
  • 69
Hydtechie
  • 41
  • 1
  • 1
    This 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
1
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Unauthorized;
throw new WebException("令牌码不正确", new InvalidTokenException());

ref:https://social.msdn.microsoft.com/Forums/en-US/f6671de3-34ce-4b70-9a77-39ecf5d1b9c3/weboperationcontext-http-statuses-and-exceptions?forum=wcf

0

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

OnlyMahesh
  • 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