简体   繁体   中英

Return 418 from WCF REST webget

I would like to know if it's possible to return a 418 from a WCF service. I've tried making an operation contract that returns a HttpResponseMessage, but that only accepts a HttpStatusCode as a parameter for status codes, and they've tragically forgotton to add 418 to that enum.

Does anyone have a solution to this? It's for a school project.

Thanks in advance

HttpStatusCode is just an enum type. I don't know why you're interested in 418 (hur hur April 1) but any integer can be cast to any enum type, so you can just return (HttpStatusCode)418 .

Since 418 is not included in the enum you can use reflection on the HttpResponseMessage object to force the HttpStatusCode property to the value 418.

Something like this:

HttpResponseMessage message;
message.GetType().GetProperty("StatusCode").SetValue(message, (object)418);

Maybe you'll even add this too ;)

    message.ReasonPhrase = "I'm a teapot.";

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