Getting a POST variable

user261863 picture user261863 · Jan 29, 2010 · Viewed 105.4k times · Source

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.

Answer

Dan Herbert picture Dan Herbert · Jan 29, 2010

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.