C# - How to list out variable names and values pos

2019-03-24 15:47发布

问题:

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!

回答1:

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] );
}


回答2:

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.



回答3:

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."



回答4:

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();


回答5:

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.



回答6:

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

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


回答7:

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



回答8:

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.



标签: c# html webforms