Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the Request Variables from an ASP.NET page

I wrote the following function that works about 95% of the time, but I need it to work 100% (obviously):

Public Shared Function getPassedVars() As String   
    Const keyCount As Integer = 54 ' 54 seems to be the number of parameter keys passed by default (for this web_app).
    '                                there are more if there is a form involved (ie. from search page)

    Dim oParams As String = ""
    Try
        With HttpContext.Current
            If .Request.Params.AllKeys.Count > keyCount Then
                For i As Integer = 0 To (.Request.Params.AllKeys.Count - (keyCount + 1))
                    oParams &= String.Format("{0}={1}{2}", .Request.Params.Keys.Item(i), .Request.Params(i), IIf(i < .Request.Params.AllKeys.Count - (keyCount + 1), ";", ""))
                Next
            End If
        End With
        Return oParams
    Catch ex As Exception
        Return Nothing
    End Try
End Function

It scrubs the Request.Params object for passed variables, which are in the beginning of the array (the remaining ones are ASP parameters). I am pretty sure I've seen a different way to get these parameters, but I haven't been able to figure it out. Any suggestions?

EDIT

So it looks like I can use the Request.URL.Query to achieve this, I will investigate this and post back.

Here is what I came up with:

Public Shared Function getPassedVars() As String
    Dim oParams As String = ""
    Dim qString As String = ""
    Dim oSplit As New List(Of String)
    Try
        With HttpContext.Current
            qString = .Request.Url.Query
            If qString.Length > 0 Then 'do we have any passed variables?
                If qString.StartsWith("?") Then qString = qString.Remove(0, 1) 'remove leading ? from querystring if it is there
                oSplit.AddRange(qString.Split("&"))
                For i As Integer = 0 To oSplit.Count - 1
                    oParams &= String.Format("{0}{1}", oSplit.Item(i), IIf(i < oSplit.Count - 1, ";", ""))
                Next
                Return oParams
            Else
                 Return Nothing
            End If
        End With
    Catch ex As Exception
        Return Nothing
    End Try
End Function

So far so good.

like image 841
Anders Avatar asked Dec 26 '08 19:12

Anders


4 Answers

Request.QueryString is a NameValueCollection, so the easiest way to get the "parameters" is to do the following:

    foreach (String s in Request.QueryString) {
        Response.Write(s + " = " + Request.QueryString[s]);
    }

Where is your function located? If it's executing in the page's code behind then you definitely do not need to use the HttpContext variable.

like image 106
NotMe Avatar answered Nov 09 '22 12:11

NotMe


It looks like you are trying to get values from the query string.

For example, for this URL:-

http://www.tempuri.org/mypage.aspx?param1=x&param2=y

I assume you want retreive the values of the query string parameters param1 and param2?

If so, just use:-

Dim param1 as String = Request.QueryString("param1")

Otherwise, if these parameters are contained in a form (an HTTP POST request) then use the method which Mitchel Sellers suggests.

like image 26
Adam Ralph Avatar answered Nov 09 '22 12:11

Adam Ralph


If you know the name you can use the following to get it by key value

Dim myParamValue as String = Request.Form("MyKeyName")

Otherwise, you can loop through the form collection, by key etc, to get the values. The key is, do you really need to be parsing all 54 items? Or are you simply looking for a few specific values?

like image 26
Mitchel Sellers Avatar answered Nov 09 '22 12:11

Mitchel Sellers


httpcontext.Current.Request.QueryString("KeyName")
like image 45
user2503764 Avatar answered Nov 09 '22 12:11

user2503764