tags:

views:

931

answers:

9

I am dynamicaly generating a HTML form that is being submitted to a .aspx webpage. How do I determine in the resulting page what variable names were submitted and what the values were? Using: Request["VarName"].toChar(); Works, but assumes that I know all of the variable names. How can I get the names and values?

Ideally, the solution would work for both POST and GET submissions...

Thank you!

+8  A: 

Have you tried something like this:

For post request:

foreach(string key in Request.Form.Keys ) 
{
   Response.Write ( Request.Form[key] );
}

For a get request:

foreach(string key in Request.QueryString.Keys ) 
{
   Response.Write ( Request.QueryString[key] );
}
JohnFx
That worked after I changed Request.Form.keys to Request.Form.Keys, and similar for Request.QueryString.Keys. Thanks!
NYARROW
Sorry. I've come to rely on that intellisense to fix my case issues to much. =)Fixed in the answer.
JohnFx
+4  A: 

Each of Request.Cookies, Request.Form, Request.QueryString and Request.ServerVariables is a NameValueCollection or similar. You can ask each of those for its keys, and proceed from there.

foreach (string key in Request.QueryString.Keys)
{
    string value = Request.QueryString[key];
    // etc
}

Quite which collections you need will depend on the purpose of this - if it's just for diagnostics, I'd be tempted to dump them all out.

Jon Skeet
Did you mean "Request.QueryString[key]", perhaps?
JohnFx
Thanks, fixed :)
Jon Skeet
A: 

Don't read from the Request collection, that is a composite collection that contains form and querystring values, but also cookies and server variables. Read from the specific collections where you want the data from, i.e. the Request.Form (for POST) and Request.QueryString (for GET).

This is how you can get all the keys and values into a string:

StringBuilder builder = new StringBuilder();
builder.AppendLine("Form values:");
foreach (string key in Request.Form.Keys) {
   builder.Append("  ").Append(key).Append(" = ").AppendLine(Request.Form[key]);
}
builder.AppendLine("QueryString values:");
foreach (string keu in Request.QueryString.Keys) {
   builder.Append("  ").Append(key).Append(" = ").AppendLine(Request.QueryString[key]);
}
string values = builder.ToString();
Guffa
+1  A: 

JohnFx's method works well. You can also include query string, server variable, and cookie data into the mix by using the Request.Params collection, as in:

foreach(string key in Request.Params.Keys)
{
  Response.Write(String.Format("{0}: {1}<br />", key, Request.Params[key]));
}

You need to be careful when using the Request.Params collection. If a QUERYSTRING variable and FORM variable both share key "Name" and have values "Val1" and "Val2", then the value of Request.Params["Name"] will be "Val1, Val2."

David Andres
A: 

You should use Request.Form as a collection. Note that multi-valued parameters (a multi-select) are turned into a collection themselves. There's an example on this page.

Cade Roux
A: 

In the declaration of the .ASPX add Trace='True'

<%@ Page Language="VB" Trace="True" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
Rob
A: 

In addition to the answers above, if you want to combine collection of QueryString, Form, ServerVariables, and Cookies items, then the best way is to use HttpRequest.Params

Braveyard
A: 

In the declaration of the .ASPX add Trace='True'

<%@ Page Language="CS" Trace="True" AutoEventWireup="false" CodeFile="Default.aspx.cs" Inherits="_Default" %>

You will be able to see the Forms data, Session, Application data etc.

Rob
A: 

If there is a , the params and form cannot get its name and value 'cause I use enctype="multipart/form-data" I can already upload the file, but for some reason I need to know the "name",how could I do?

Lynn