c# - .NET 框架中是否有针对不同 Web 方法类型(GET、PUT、POST、DELETE、HEAD)的常量?

标签 c# .net wcf constants

我刚刚在创建 RESTful WCF 服务时注意到 WebInvoke 属性上的 Method 参数区分大小写(需要大写)。

所以,

[WebInvoke(Method = "Delete")]

不等于

[WebInvoke(Method = "DELETE")]

这个错误导致了 ProtocolException:

System.ServiceModel.ProtocolException: The remote server returned an unexpected response: (405) Method Not Allowed.

我想知道我应该使用 .NET 框架中的一组常量来代替上面示例中的“DELETE”。我当然可以定义我自己的一组常量,但如果感觉像是框架中可能存在的东西,我只是想念它。

最佳答案

有点间接,但是有System.Net.WebRequestMethods.Http常量:

public const string Connect = "CONNECT";
public const string Get = "GET";
public const string Head = "HEAD";
public const string MkCol = "MKCOL";
public const string Post = "POST";
public const string Put = "PUT";

但没有“DELETE”——建议您自己制作...

恼人的是,有一个System.Web.HttpVerb,但它是内部的,所以不可用——而且它是一个枚举,所以要在一个属性你需要一点 hackery。

关于c# - .NET 框架中是否有针对不同 Web 方法类型(GET、PUT、POST、DELETE、HEAD)的常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/500507/

相关文章:

c# - 网络协议(protocol)的实现

c# - 具有多个 dbcontext 的一个事务

c# - WCF 服务返回 400 错误 : The body of the message cannot be read because it is empty

c# - spring.net有什么用?

wcf - Azure Service Fabric 双工连接

c# - 重新使用 GDI+ 对象是不好的做法吗? (或 : How to use many nested using blocks without getting headaches? )

c# - 如何添加对 IRunnableTask 的引用

c# - 如何在 C# 中将时区作为字符串获取?

c# - SqlCommand 在 C# 中创建数据库竞争条件

c# - CloudTableClient.CreateCloudTableClient 抛出 System.IO.FileNotFoundException 类型的异常