简体   繁体   English

从WCF REST Webget返回418

[英]Return 418 from WCF REST webget

I would like to know if it's possible to return a 418 from a WCF service. 我想知道是否可以从WCF服务返回418。 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. 我尝试过制定一个返回HttpResponseMessage的操作协定,但该协定仅接受HttpStatusCode作为状态代码的参数,而他们却悲惨地忘记为该枚举添加418。

Does anyone have a solution to this? 有人对此有解决方案吗? It's for a school project. 这是一个学校项目。

Thanks in advance 提前致谢

HttpStatusCode is just an enum type. HttpStatusCode只是一个enum类型。 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 . 我不知道您为什么对418(4月1日,星期四)感兴趣,但是任何整数都可以转换为任何枚举类型,因此您只需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. 由于418不包含在枚举中,因此可以对HttpResponseMessage对象使用反射,以将HttpStatusCode属性强制为值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.";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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