How to get POST and GET data from a FORM in ASP.NE

2019-05-19 17:23发布

I need to get an understanding about how you can handle get and post data in a form in asp.net in these 2 situations:

You submit a form with GET method: 
action: "form.php" 
parameters: text1=test

You submit a form with POST method: 
action: "form.php?text1=sometext" 
parameters: text1=somedifferenttext

I know these 3 commands:

String val1 = Page.Request["text1"];
String val2 = Page.Request.Form["text1"];
String val3 = Page.Request.QueryString["text1"];

I wonder what are the exact commands to access get and post variables directly?

标签: asp.net forms
3条回答
Summer. ? 凉城
2楼-- · 2019-05-19 17:40

For a GET, Page.Request.RawUrl will get you the original querystring. You need to parse the whole URL to get it.

If it's a POST, read it from Page.Request.InputStream

查看更多
我命由我不由天
3楼-- · 2019-05-19 17:49

Get variables are stored in the query string:

String getText1 = Page.Request.QueryString["text1"];

Post variables are stored in the form:

String postText1 = Page.Request.Form["text1"];

If you want to know more about the difference between Get and Post variables, I'd suggest having a read of this question: When do you use POST and when do you use GET?

查看更多
小情绪 Triste *
4楼-- · 2019-05-19 17:55

It might also be useful to know that both Page.Request.Form and Page.Request.QueryString are NameValueCollection objects. So if you want to iterate over their keys, you can use Page.Request.Form.Keys and Page.Request.QueryString.Keys.

查看更多
登录 后发表回答