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

时间:2009-02-01 09:47:46

标签: c# .net wcf constants

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

所以,

[WebInvoke(Method = "Delete")]

不等于

[WebInvoke(Method = "DELETE")]

此错误导致ProtocolException

  

System.ServiceModel.ProtocolException:远程服务器返回了意外响应:(405)Method Not Allowed。

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

2 个答案:

答案 0 :(得分:12)

有点间接,但有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";

但没有“删除” - 建议你制作自己的......

令人讨厌的是,有一个System.Web.HttpVerb,但它是internal,所以不可用 - 它是一个枚举,所以要在属性中使用你需要一些hackery的名字。

答案 1 :(得分:1)

System.Web.Mvc命名空间有HttpVerbs。 https://msdn.microsoft.com/en-us/library/system.web.mvc.httpverbs(v=vs.118).aspx 可以在Assemblies,Extensions

下选择引用