如何返回HTTP 429? [英] How to return HTTP 429?

查看:1477
本文介绍了如何返回HTTP 429?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WCF实现API,规范说在某些情况下返回HTTP 429.

I'm implementing an API using WCF and the specification says to return HTTP 429 in certain circumstances.

通常我只写:

throw new WebFaultException(HttpStatusCode.NotFound);

然而,HttpStatusCode枚举不包含429。

However the HttpStatusCode enum does not contain a 429.

我显然可以转换为enum

I can obviously cast to the enum

throw new WebFaultException((HttpStatusCode)429);

但是我担心这不会为调用我的API的应用程序产生正确的结果。

However I'm worried that this will not produce the correct result to the application calling my API.

创建扩展HttpStatusCode并发送有效(但不支持的)HTTP状态的最佳方法是什么?

What's the best way to create extend the HttpStatusCode and send valid (but unsupported) HTTP statuses?

推荐答案

C#语言规范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.

所以这完全没问题,这将是你最好的选择:

So this is completely alright to do and would be your best bet:

throw new WebFaultException((System.Net.HttpStatusCode)429);

这篇关于如何返回HTTP 429?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆