C# - How to list out variable names and values posted to ASPX page

后端 未结 8 2427
春和景丽
春和景丽 2021-02-13 10:11

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 value

8条回答
  •  既然无缘
    2021-02-13 10:42

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

提交回复
热议问题