0

Possible Duplicate:
Getting a POST Variable in ASP.net

I just want to print all posted variables with their values.

Request.Form gives me only names with no values and Request.InputStream gives me the "Timeouts are not supported on this stream." error.

How can I get all posted values like $_POST does in PHP?

Community
  • 1
  • 1
Stan
  • 25,744
  • 53
  • 164
  • 242

3 Answers3

6

Request.Form is a NameValueCollection, you can access posted data like this:

string postedName = Request.Form["name"];
Zbigniew
  • 27,184
  • 6
  • 59
  • 66
  • This is bad. I just want to see all the post data w/o tinkering what variables I actually post. – Stan Jan 16 '13 at 17:35
  • 1
    This is good, this is how you get values via post,your question is not clear :/ What do you mean by seeing, you want to see them in (VS) debugger? – Zbigniew Jan 16 '13 at 17:37
  • No, I'm using NewtonJSON to output them like so `return Content(JsonConvert.SerializeObject(Request.Form));` I can see them in debugger but I just want to print them out. I don't believe there is no way to do that. – Stan Jan 16 '13 at 17:47
  • It seems that this is serialization problem, take a look at this: [how to convert NameValueCollection to JSON string?](http://stackoverflow.com/questions/7003740/how-to-convert-namevaluecollection-to-json-string) – Zbigniew Jan 16 '13 at 18:02
1

Just use Request, like so:

var tmp = Request["formfield"]; // gets the value of 'formfield' from the request

However, unlike in PHP, keep in mind that the Request variable will contain both GET and POST parameters.

Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177
1

If you want to view all Form key-value pairs for debugging you can do something like this:

var dict = new Dictionary<string, string>();
foreach (string key in Form.Keys)
    dict.Add(key, Form[key]);

Then set a breakpoint after the loop and inspect the dictionary.

Shmiddty
  • 13,847
  • 1
  • 35
  • 52
  • Alternatively, you can dig into the non-public members to find the values, but it's generally more of a hassle than it's worth. – Shmiddty Jan 16 '13 at 17:58
  • error: 'System.Web.UI.HtmlControls.HtmlForm' does not contain a definition for 'Keys' (in Form.Keys) – Cobaia Dec 09 '13 at 18:53