Hcabnettek
Hcabnettek

Reputation: 12928

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.

Upvotes: 17

Views: 87836

Answers (5)

Michael Ciba
Michael Ciba

Reputation:

Not sure about the syntax in VB.NET but in C# you would just need to do

StringId = Request.QueryString.Get("d");

Hope this helps.

Upvotes: 1

Yustian
Yustian

Reputation: 31

try this

Dim name As String = System.IO.Path.GetFileName(Request.ServerVariables("SCRIPT_NAME"))
Dim qrystring As String = Request.ServerVariables("QUERY_STRING")
Dim fullname As String = name & "/" & qrystring

Upvotes: 1

CraigTP
CraigTP

Reputation: 44909

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.

Upvotes: 27

Dillie-O
Dillie-O

Reputation: 29725

In VB.Net you can do it with the following.

Dim id As String = Request.Params("RequestId")

If you want to process this in as an integer, you can do the following:

Dim id As Integer

If Integer.TryParse(Request.Params("RequestId"), id) Then
   DoProcessingStuff()
End If

Upvotes: 8

Sani Huttunen
Sani Huttunen

Reputation: 24385

Try this:

so.Cpage = "ContractChange.aspx?" & Request.RawUrl.Split("?")(1)

Upvotes: 10

Related Questions