Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the URL and Querystring? vb.net

I am refactoring some legacy code. The app was not using querystrings. The previous developer was hard coding some variables that the app uses in other places.

Like this using VB.NET

 so.Cpage = "ContractChange.aspx"

My question is can I programatically set this value and include the current querystring?

I want so.Cpage to be something like ContractChange.aspx?d=1&b=2

Can I do this with the request object or something? Note, I don't need the domain.

like image 519
Hcabnettek Avatar asked Jul 07 '09 15:07

Hcabnettek


2 Answers

To get the current query string you would simply do something like the following:

Dim query as String = Request.QueryString("d")

This will assign the value of the "d" querystring to the string variable "query". Note that all query string values are strings, so if you're passing numbers around, you'll need to "cast" or convert those string values to numerics (be careful of exceptions when casting, though). For example:

Dim query as String = Request.QueryString("d")
Dim iquery as Integer = CType(query, Integer)

The QueryString property of the Request object is a collection of name/value key pairs. Specifically, it's of type System.Collections.Specialized.NameValueCollection, and you can iterate through each of the name/value pairs as so:

Dim coll As System.Collections.Specialized.NameValueCollection = Request.QueryString
Dim value As String
For Each key As String In coll.AllKeys
   value = coll(key)
Next

Using either of these mechanisms (or something very similar) should enable you to construct a string variable which contains the full url (page and querystrings) that you wish to navigate to.

like image 118
CraigTP Avatar answered Oct 02 '22 23:10

CraigTP


Try this:

so.Cpage = "ContractChange.aspx?" & Request.RawUrl.Split("?")(1)
like image 43
Sani Singh Huttunen Avatar answered Oct 03 '22 00:10

Sani Singh Huttunen