c# - 如何返回 HTTP 429?

标签 c# http wcf http-status-code-429

我正在使用 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/

相关文章:

c# - 从代码启动音乐应用程序

c# - Band 应用程序调用了为不同线程编码的接口(interface)

http - Clojure 黑色 : how to set up basic authentication for only a part of the application

c# - 没有证书存储的 WCF 证书

c# - 更改数组大小

c# - 为什么 Dictionary<TKey, TValue> 不支持空键?

HTTP 验证所有 Varnish 请求

c# - 通过 HttpWebRequest 传递二进制数据

java - Eclipse Android 插件在 basicHttpBinding 中调用 WCF Web 服务

wcf - 为什么在 ASP.NET MVC 中有 JsonResult 时使用 WCF REST?