简体   繁体   中英

How do I return HTTP status code 420 from my WCF service?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM