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

后端 未结 8 2303
春和景丽
春和景丽 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:18

    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.

    0 讨论(0)
  • 2021-02-13 10:20

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

    <%@ Page Language="VB" Trace="True" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    
    0 讨论(0)
  • 2021-02-13 10:22

    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.

    0 讨论(0)
  • 2021-02-13 10:26

    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] );
    }
    
    0 讨论(0)
  • 2021-02-13 10:32

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

    0 讨论(0)
  • 2021-02-13 10:37

    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.

    0 讨论(0)
提交回复
热议问题