1

My client is requesting to set status code=420 if the WCF service returns an error. How can i achieve it?

Is this even possible? I tried to set HTTP status code

WebOperationContext ctx = WebOperationContext.Current;
ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.BadRequest;

I cannot find status code 420 in HttpStatusCode.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Arti
  • 2,993
  • 11
  • 68
  • 121
  • http://stackoverflow.com/questions/140104/how-can-i-return-a-custom-http-status-code-from-a-wcf-rest-method : If your solution does not work, one of the comments in the answer above suggests throwing a WebFaultException http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webfaultexception(v=vs.110).aspx – Tewr Mar 04 '14 at 06:46
  • 420 is not part of the standard – David Pilkington Mar 04 '14 at 06:47
  • Are you using Spring or Twitter? – David Pilkington Mar 04 '14 at 06:47
  • also http://stackoverflow.com/questions/2022887/how-can-i-createa-custom-http-status-code-from-a-wcf-rest-method – Tewr Mar 04 '14 at 06:50
  • Yes it is possible if you host your service in IIS. – danish Mar 04 '14 at 06:51
  • @Tewr 420 is not part of the standard HTTP status code. SO I cannot get it to return 420 – Arti Mar 04 '14 at 07:00
  • @DavidPilkington I am using WCF ASP.net to build my API. – Arti Mar 04 '14 at 07:01
  • Is it necessary to send the status code 420 only...or you can send others too? – Jivan Mar 04 '14 at 07:01
  • My client requested me to send status code 420. If it it not possible then i can speak to the client. – Arti Mar 04 '14 at 07:10
  • You might want to check the caller's location based on their IP and alternatively return a 403 (Forbidden) instead of 420. – Steve Konves Mar 04 '14 at 07:31

1 Answers1

5

You can cast any int value to an enum (well, one with int as its base type) - it doesn't have to be a "valid" value - so you can try:

ctx.OutgoingResponse.StatusCode = (System.Net.HttpStatusCode)420;

See C# Language Specification, version 5, section 1.10:

The set of values that an enum type can take on is not limited by its enum members. In particular, any value of the underlying type of an enum can be cast to the enum type and is a distinct valid value of that enum type.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448