.NET 框架中是否有针对不同 Web 方法类型(GET、PUT、POST、DELETE、HEAD)的常量?
我刚刚注意到在创建 RESTful WCF 服务时,WebInvoke
属性上的 Method 参数区分大小写(需要大写)。
因此,
[WebInvoke(Method = "Delete")]
不等于此
[WebInvoke(Method = "DELETE")]
错误导致了 ProtocolException
:
System.ServiceModel.ProtocolException:远程服务器返回意外响应:(405) 不允许方法。
我想知道 .NET 框架中是否有一组常量,我应该使用它们来代替上面示例中的“DELETE”。 我当然可以定义自己的常量集,但如果感觉像是框架中可能存在的东西,而我只是想念它。
I just noticed while creating a RESTful WCF service that the Method parameter on the WebInvoke
attribute is case sensitive (CAPS required).
So,
[WebInvoke(Method = "Delete")]
is not equal to
[WebInvoke(Method = "DELETE")]
This mistake was causing a ProtocolException
:
System.ServiceModel.ProtocolException: The remote server returned an unexpected response: (405) Method Not Allowed.
I was wondering is there a set of constants in the .NET framework that I should be using in place of "DELETE" in the above example. I could of course define my own set of constants, but if feels like something that probably exists in the framework and I am just missing it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有点间接,但有 System.Net.WebRequestMethods.Http 常量:
但没有“DELETE” - 建议您自己制作...
令人烦恼的是,有一个 System.Web.HttpVerb ,但它是
内部
,所以不可用 - 而且它是一个枚举,因此要在属性中使用该名称,您需要一些技巧。A bit indirect, but there are
System.Net.WebRequestMethods.Http
constants:but no "DELETE" - suggest you make your own...
Annoyingly, there is a
System.Web.HttpVerb
, but it isinternal
, so not usable - and it is an enum, so to use the name in an attribute you'd need a bit of hackery.System.Web.Mvc 命名空间具有 HttpVerbs。
https://msdn .microsoft.com/en-us/library/system.web.mvc.httpverbs(v=vs.118).aspx
可以在程序集、扩展下选择参考
The System.Web.Mvc namespace has HttpVerbs.
https://msdn.microsoft.com/en-us/library/system.web.mvc.httpverbs(v=vs.118).aspx
The reference can be selected under Assemblies, Extensions