How can I return a custom HTTP status code from a WCF REST method?

C#.NetWcfRest

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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;

Solution 2 - C#

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

Solution 3 - C#

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.

Solution 4 - C#

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

Solution 5 - C#

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

Solution 6 - C#

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

Solution 7 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkgriffsView Question on Stackoverflow
Solution 1 - C#Eric SchoonoverView Answer on Stackoverflow
Solution 2 - C#Graeme BradburyView Answer on Stackoverflow
Solution 3 - C#JarrettVView Answer on Stackoverflow
Solution 4 - C#eitzoView Answer on Stackoverflow
Solution 5 - C#HydtechieView Answer on Stackoverflow
Solution 6 - C#user5234326View Answer on Stackoverflow
Solution 7 - C#OnlyMaheshView Answer on Stackoverflow