C# .NET 中“HttpRequest.RequestType”和“WebRequest.Method”值的常量在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/277884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where is the constant for "HttpRequest.RequestType" and "WebRequest.Method" values in .NET?
提问by Dan Herbert
I need to check the RequestType
of an HttpRequest
in ASP.NET (or WebRequest.Method
). I know that I can just use the string values "POST
" or "GET
" for the request type, but I could have sworn there was a constant somewhere in some class in .NET that contained the values.
我需要检查RequestType
的HttpRequest
在ASP.NET(或WebRequest.Method
)。我知道我可以只使用字符串值“ POST
”或“ GET
”作为请求类型,但我可以发誓在包含这些值的 .NET 中某个类的某处有一个常量。
Out of curiosity I was wondering if anyone knew what class these string constants for GET
and POST
were in. I've tried searching online but I've had no luck, so I thought I'd ask here.
出于好奇,我想知道是否有人知道这些字符串常量用于哪个类GET
并且POST
在哪个类中。我试过在线搜索但我没有运气,所以我想我会在这里问。
采纳答案by Marc Gravell
System.Net.WebRequestMethods.Http
.Connect = "CONNECT"
.Get = "GET"
.Head = "HEAD"
.MkCol = "MKCOL"
.Post = "POST"
.Put = "PUT"
Ultimately, though; since const
expressions are burned into the caller, this is identical to using "GET" etc, just without the risk of a typo.
不过,最终;由于const
表达式被刻录到调用者中,这与使用“GET”等相同,只是没有输入错误的风险。
回答by Marc Gravell
In ASP.NET MVC they're in System.Web.Mvc.HttpVerbs. But all methods that take one of these enum values also has a text override, as there is no complete set of HTTP verbs, only a set of currently defined values (see hereand hereand here).
在 ASP.NET MVC 中,它们位于System.Web.Mvc.HttpVerbs 中。但是,采用这些枚举值之一的所有方法也具有文本覆盖,因为没有完整的 HTTP 动词集,只有一组当前定义的值(请参阅此处和此处以及此处)。
You can't create an enumeration that covers all verbs, as there is the possibility that verbs can be added, and enumerations have versioning issuesthat make this impractical.
您无法创建涵盖所有动词的枚举,因为有可能添加动词,并且枚举具有版本控制问题,因此不切实际。
回答by xmedeko
Also exists System.Net.Http.HttpMethod
which can serve instead of enum. You can compare them aMethod == HttpMethod.Get
, etc. To get string method name call e.g. HttpMethod.Get.Method
.
也存在System.Net.Http.HttpMethod
可以代替枚举的服务。您可以比较它们aMethod == HttpMethod.Get
等。要获取字符串方法名称,请调用例如HttpMethod.Get.Method
。
回答by B12Toaster
In ASP.NET Coreyou will find a collection of http method strings in the HttpMethods.csclass under the Microsoft.AspNetCore.Http
namespace.
在ASP.NET Core 中,您将在命名空间下的HttpMethods.cs类中找到一组 http 方法字符串Microsoft.AspNetCore.Http
。
This class also offers boolean helpers such as IsGet()
or IsPost()
for better semantics.
此类还提供布尔帮助程序,例如IsGet()
或IsPost()
以获得更好的语义。
Please note that these strings are exposed as public static readonly string
and not as constants
.
请注意,这些字符串公开为 ,public static readonly string
而不是constants
。