92

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?

1
  • 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? Commented Jul 18, 2012 at 22:56

7 Answers 7

122

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;
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Doesn't work in my case, the status gets overwritten. Throwing a WebFaultException, however, does seem to work.
79

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 );

4 Comments

I like this better than the accepted one since we are not using the static WebOperationContext.Current
keep in mind this is only valid since famework 4 msdn.microsoft.com/en-us/library/dd989924.aspx
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.
This seems great for anything outside of the 2XX codes, but would you throw a WebFaultException to return an HttpStatusCode.Created?
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.

1 Comment

Is this preferred over the method displayed in the accepted answer?
7

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";

Comments

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;
}

1 Comment

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?
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

Comments

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

1 Comment

That doesn't work for me either, I just get a 400 Bad Request, with no other useful information, and no response body.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.