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

发布于 2024-07-13 04:59:44 字数 472 浏览 7 评论 0原文

我刚刚注意到在创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事和酒 2024-07-20 04:59:44

有点间接,但有 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 ,但它是内部,所以不可用 - 而且它是一个枚举,因此要在属性中使用该名称,您需要一些技巧。

A bit indirect, but there are System.Net.WebRequestMethods.Http constants:

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";

but no "DELETE" - suggest you make your own...

Annoyingly, there is a System.Web.HttpVerb, but it is internal, so not usable - and it is an enum, so to use the name in an attribute you'd need a bit of hackery.

假扮的天使 2024-07-20 04:59:44

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文