ASP.NET Enumerate Query String Values
Although it is fairly easily to get a single query string value, I recently found it was very difficult to get the entire querystring which I wanted to include in an error report. In order to store the querystring in a variable you need to create a NameValueCollection from the System.Collections.Specialized namespace:
<%@ Import Namespace="System.Collections.Specialized"%>
Dim param As NameValueCollectionparam = Request.QueryString
Then you will need two For loops (i.e. nested loops) to get the collection’s keys and values:
Dim i, j As Integer
Dim arr1() As String = param.AllKeys
For i = 0 To arr1.Length - 1
Dim arr2() As String = param.GetValues(arr1(i))
For j = 0 To arr2.Length - 1
strQueryStringValue = strQueryStringValue & arr1(i) & “=” & arr2(j) & “&”
Next
Next
This problem came up because I was trying to troubleshoot some code which I did not write. The previous developer gave a form the GET method and was passing a memo field in the querystring. This is bad programming because the querystring is not meant for lengthy amounts of text. There is a limit to the allowable length of a querystring. Many firewalls will truncate a querystring because lengthy querystrings are used to cause buffer overflows in web servers and browsers. Therefore I wanted to get the length of the entire querystring for my error report.
1 Comments:
Single Loop if sufficient if each key was only used once. For Each is also cleaner.
For Each strKey As String In Request.QueryString.Keys
system.diagnostics.trace.WriteLine(strKey & " " & Request.QueryString(strKey))
Next
Post a Comment
<< Home