Search This Blog

Monday, August 13, 2012

Get Current Url ASP.NET (VB.NET)

ASP.NET > Get Current Url

The ServerVariables collection is used to retrieve the server variable values.

SERVER_NAME: Returns the server's host name, DNS alias, or IP address
SERVER_PORT: Returns the port number to which the request was sent
SCRIPT_NAME: Returns a virtual path to the script being executed

Example: Get Current Url with parameters

Public Shared Function GetCurrentUrl()
' currentUrl
 Dim currentUrl = "http://" &HttpContext.Current.Request.ServerVariables  ("SERVER_NAME").ToString()

 If HttpContext.Current.Request.ServerVariables("SERVER_PORT").ToString() <> "" Then
    currentUrl = currentUrl + ":" + HttpContext.Current.Request.ServerVariables("SERVER_PORT").ToString()
  End If
  currentUrl = currentUrl + HttpContext.Current.Request.ServerVariables ("SCRIPT_NAME").ToString()
 If HttpContext.Current.Request.QueryString.ToString() <> "" Then
   currentUrl += "?" + HttpContext.Current.Request.QueryString.ToString()
 End If
 Return currentUrl
End Function