Return all QueryString and Form elements (C# / VB.Net)

Posted by Blake on 2/7/2012
)

This example will populate a StringBuilder with all of the name/value pairs from the current Form post or QueryString. I’m providing both VB.Net and C# in this example. Since I was using this for debugging, I also included some HTML that you can strip out.

VB.Net

Dim sb As New StringBuilder()
sb.AppendLine("Form Elements:<br /><br />")
For Each key As String In HttpContext.Current.Request.Form.AllKeys
    sb.AppendFormat("{0}={1}<br />", key, HttpContext.Current.Request.Form(key))
Next
sb.AppendLine("QueryString Elements:<br /><br />")
For Each key As String In context.Request.QueryString.AllKeys
    sb.AppendFormat("{0}={1}<br />", key, HttpContext.Current.Request.QueryString(key))
Next

C#

StringBuilder sb = new StringBuilder();
sb.AppendLine("Form Elements:<br /><br />");
foreach (string key in HttpContext.Current.Request.Form.AllKeys)
{
    sb.AppendFormat("{0}={1}<br />", key, HttpContext.Current.Request.Form[key]);
}
sb.AppendLine("QueryString Elements:<br /><br />");
foreach (string key in context.Request.QueryString.AllKeys)
{
    sb.AppendFormat("{0}={1}<br />", key, HttpContext.Current.Request.QueryString[key]);
}