C# 获取 POST 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2162495/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:03:24  来源:igfitidea点击:

Getting a POST variable

c#asp.nethttp-post

提问by user261863

I am using C# with ASP.NET.

我在 ASP.NET 中使用 C#。

How do I check if a parameter has been received as a POST variable?

如何检查参数是否已作为 POST 变量接收?

I need to do different actions if the parameter has been sent via POST or via GET.

如果参数是通过 POST 或通过 GET 发送的,我需要执行不同的操作。

采纳答案by Dan Herbert

Use this for GET values:

将其用于 GET 值:

Request.QueryString["key"]

And this for POST values

这对于 POST 值

Request.Form["key"]

Also, this will work if you don't care whether it comes from GET or POST, or the HttpContext.Itemscollection:

此外,如果您不关心它是来自 GET 还是 POST 或HttpContext.Items集合,这将起作用:

Request["key"]

Another thing to note (if you need it) is you can check the type of request by using:

另一件需要注意的事情(如果你需要的话)是你可以使用以下命令检查请求的类型:

Request.RequestType

Which will be the verb used to access the page (usually GET or POST). Request.IsPostBackwill usually work to check this, but only if the POST request includes the hidden fields added to the page by the ASP.NET framework.

这将是用于访问页面的动词(通常是 GET 或 POST)。Request.IsPostBack通常会检查这一点,但前提是 POST 请求包含由 ASP.NET 框架添加到页面的隐藏字段。

回答by egyedg

Use the

使用

Request.Form[]

申请表[]

for POST variables,

对于 POST 变量,

Request.QueryString[]

请求.QueryString[]

for GET.

忘记。

回答by Wim Hollebrandse

In addition to using Request.Formand Request.QueryStringand depending on your specific scenario, it may also be useful to check the Page's IsPostBackproperty.

除了根据您的特定场景使用Request.Form和之外,Request.QueryString检查PageIsPostBack属性也可能很有用。

if (Page.IsPostBack)
{
  // HTTP Post
}
else
{
  // HTTP Get
}