我正在使用 WCF 实现 API,规范说明在某些情况下返回 HTTP 429。
通常我会简单地写:
throw new WebFaultException(HttpStatusCode.NotFound);
但是 HttpStatusCode 枚举不包含 429。
我显然可以转换为枚举
throw new WebFaultException((HttpStatusCode)429);
但是我担心这不会为调用我的 API 的应用程序产生正确的结果。
创建扩展 HttpStatusCode 并发送有效(但不受支持)HTTP 状态的最佳方法是什么?
最佳答案
来自C# Language Specification 5.0 :
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.
所以这是完全可以做的,也是你最好的选择:
throw new WebFaultException((System.Net.HttpStatusCode)429);
关于c# - 如何返回 HTTP 429?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22636602/