44

I am using C# with ASP.NET.

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

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

abatishchev
  • 98,240
  • 88
  • 296
  • 433
user261863
  • 443
  • 1
  • 4
  • 4

3 Answers3

101

Use this for GET values:

Request.QueryString["key"]

And this for POST values

Request.Form["key"]

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

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.IsPostBack will usually work to check this, but only if the POST request includes the hidden fields added to the page by the ASP.NET framework.

Dan Herbert
  • 99,428
  • 48
  • 189
  • 219
  • 3
    I would not use Request["key"]...as this is the Items collection and gets a list of all objects stored in the page context. This would also include cookie values. – Darren Oct 15 '12 at 01:24
7

Use the

Request.Form[]

for POST variables,

Request.QueryString[]

for GET.

egyedg
  • 724
  • 1
  • 6
  • 13
1

In addition to using Request.Form and Request.QueryString and depending on your specific scenario, it may also be useful to check the Page's IsPostBack property.

if (Page.IsPostBack)
{
  // HTTP Post
}
else
{
  // HTTP Get
}
Wim
  • 11,998
  • 1
  • 34
  • 57