Getting a POST variable

戏子无情 提交于 2019-11-26 10:57:30

问题


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.


回答1:


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.




回答2:


Use the

Request.Form[]

for POST variables,

Request.QueryString[]

for GET.




回答3:


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
}


来源:https://stackoverflow.com/questions/2162495/getting-a-post-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!