views:

3538

answers:

4

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.

A: 

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?

Mitchel Sellers
I dont necessarily know the names of the Variables, although I use about 5 different ones across all my pages. If you read the loop, it only goes thru the first few (delimited by my keyCount var) rather than all of them
Anders
A: 

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&amp;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.

AdamRalph
A: 

Request.Params will contain the query parameters you're after.

There's no need to parse the info from Request.URL since it's already done for you.

Todd Smith
+2  A: 

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.

Chris Lively
It is in a Functions.vb file inside my APP_CODE folder, so the HttpContext.Current is necessary. Thanks for the input, that loop may come in handy in the near future :)
Anders