WebPaths - Get Various Request Properties - January 2012
Updated May 2016

If you download this tip please consider making a modest donation.

This tip-of-the-month demonstrates how to get an application's root URL. I searched using Google and could not find a way to do it. So this becomes the January 2012 tip-of-the-month. While creating the code I tried using a number of Request methods so I documented the results in this tip. Click here to run the demo application.

May 2016 - I revised the tip to include browser information to determine if a MobileDevice, iPad and iPhone are being accessed.

The Demo Application

The Asp.Net, VB.Net, VS/2008 project WebPaths is documented using screen captures:

I added a method to class cFilenameParse which returns the root application URL.
In this example: http://larryrebichtips.net.
See below for instructions on how to use the method.

These Request Methods

Various Browser Capabilities are shown here.

Operative Code

Dim oFP As New cFilenameParse
Dim sWebRoot = oFP.AppPathAsWeb(bWithBackslash)
Private Function AppPathAsWeb(ByVal bWithBackslash As Boolean) As String
sAbsolutePath As String = HttpContext.Current.Request.Url.AbsolutePath
    Dim sAbsoluteUri As String = HttpContext.Current.Request.Url.AbsoluteUri
    sAbsoluteUri = RemoveQueryString(sAbsoluteUri)
'2011/12/09 Added
    Dim sAbsoluteRoot As String = Replace(sAbsoluteUri, sAbsolutePath, "")
    If bWithBackslash Then
        sAbsoluteRoot = Me.AddBackslash(sAbsoluteRoot)
        sAbsoluteRoot = Replace(sAbsoluteRoot,
"\", "/")
    End If
End Function
The AbsolutePath is removed from AbsoluteUri to get the WebRoot.
Any query string is also removed using function RemoveQueryString.
See the source code for details.

Download the VB.Net Code

Download the source code by clicking on the zipped file name. Click here to run the demo application.

Download - Source Zipped File Size
VS/2008 VB.Net, ASP.Net Solution WebPaths.zip 88k

Previous Tip    Tip Index    Next Tip


Updated 2016/05/27