Jeff,
Some people look at the response headers and get the base path from there.
But, unfortunately I have seen errors in getting the path this way depending
on the browser connecting up to the site. I have also seen similar errors
when getting the same type of path via javascript.
I finally decided (about three years ago) when I began working in .net to
create an "ApplicationObject" in which I set an environment variable to
development, localhost, staging, or production. The application object also
has public properties where I specify the base url and the base file path
for each of the forementioned environments.
Then, whenever I create an object that uses one of those base paths I tie it
to the application object. I now have a pageutilities object which creates a
url from the base path of the application object and bases it on the paths
stored there.
Here is the code for the two objects:
''' <summary>
''' Controls the behavior of other Fortunate Class Library objects via the
environment parameter and stores static properties for use in the
application.
''' </summary>
Public Class Application
#Region "Global Private Variables"
Private _ApplicationEnvironment As EnvironmentValues =
EnvironmentValues.LocalHost
Private _ApplicationType As ApplicationTypes = ApplicationTypes.Internet
Private _ApplicationName As String
Private _BaseFilePathDevelopment As String
Private _BaseFilePathLocalhost As String
Private _BaseFilePathStaging As String
Private _BaseFilePathProduction As String
Private _BaseUriDevelopment As String
Private _BaseUriLocalHost As String
Private _BaseUriStaging As String
Private _BaseUriProduction As String
#End Region
#Region "Public Enum Lists"
''' <summary>
''' A list of environment modes.
''' </summary>
Public Enum EnvironmentValues
''' <summary>
''' Used during intial testing on a development machine. Error messages will
be delivered directly to screen.
''' </summary>
Development
''' <summary>
''' Used during development on the developer's own machine. Error messages
will be delivered directly to screen.
''' </summary>
LocalHost
''' <summary>
''' Used for production environment. Error messages are sent to the
developer and only user friendly error messages will be displayed on screen.
''' </summary>
Production
''' <summary>
''' Used during secondary testing on a staging machine. Error messages will
be delivered to the developer and a tester friendly error message displayed
on screen.
''' </summary>
Staging
End Enum
''' <summary>
''' A list of application types.
''' </summary>
Public Enum ApplicationTypes
''' <summary>
''' A web based application.
''' </summary>
Internet
''' <summary>
''' A client based application.
''' </summary>
Client
''' <summary>
''' An intranet based application.
''' </summary>
Intranet
''' <summary>
''' A server based application.
''' </summary>
Server
End Enum
#End Region
#Region "Public Properties"
''' <summary>
''' Gets or sets the application's operating mode.
''' </summary>
Public Property ApplicationEnvironment() As EnvironmentValues
Get
Return _ApplicationEnvironment
End Get
Set(ByVal Value As EnvironmentValues)
_ApplicationEnvironment = Value
End Set
End Property
''' <summary>
''' Gets or sets the application's type.
''' </summary>
Public Property ApplicationType() As ApplicationTypes
Get
Return _ApplicationType
End Get
Set(ByVal Value As ApplicationTypes)
_ApplicationType = Value
End Set
End Property
''' <summary>
''' Gets or sets the application's name.
''' </summary>
Public Property ApplicationName() As String
Get
Return _ApplicationName
End Get
Set(ByVal Value As String)
_ApplicationName = Trim(Value)
End Set
End Property
''' <summary>
''' Gets or sets the base uri to be used for link creation in development
mode.
''' </summary>
Public Property BaseFilePathDevelopment() As String
Get
Return _BaseFilePathDevelopment
End Get
Set(ByVal Value As String)
_BaseFilePathDevelopment = Value
End Set
End Property
''' <summary>
''' Gets or sets the base uri to be used for link creation in localhost
mode.
''' </summary>
Public Property BaseFilePathLocalhost() As String
Get
Return _BaseFilePathLocalhost
End Get
Set(ByVal Value As String)
_BaseFilePathLocalhost = Value
End Set
End Property
''' <summary>
''' Gets or sets the base uri to be used for link creation in staging mode.
''' </summary>
Public Property BaseFilePathStaging() As String
Get
Return _BaseFilePathStaging
End Get
Set(ByVal Value As String)
_BaseFilePathStaging = Value
End Set
End Property
''' <summary>
''' Gets or sets the base uri to be used for link creation in production
mode.
''' </summary>
Public Property BaseFilePathProduction() As String
Get
Return _BaseFilePathProduction
End Get
Set(ByVal Value As String)
_BaseFilePathProduction = Value
End Set
End Property
''' <summary>
''' Gets or sets the base file path to be used for file access in
development mode.
''' </summary>
Public Property BaseUriDevelopment() As String
Get
Try
Return _BaseUriDevelopment
Catch e As Exception
Throw e
End Try
End Get
Set(ByVal Value As String)
Try
_BaseUriDevelopment = Trim(Value)
Catch e As Exception
Throw e
End Try
End Set
End Property
''' <summary>
''' Gets or sets the base file path to be used for file access in localhost
mode.
''' </summary>
Public Property BaseUriLocalHost() As String
Get
Try
Return _BaseUriLocalHost
Catch e As Exception
Throw e
End Try
End Get
Set(ByVal Value As String)
Try
_BaseUriLocalHost = Trim(Value)
Catch e As Exception
Throw e
End Try
End Set
End Property
''' <summary>
''' Gets or sets the base file path to be used for file access in staging
mode.
''' </summary>
Public Property BaseUriStaging() As String
Get
Try
Return _BaseUriStaging
Catch e As Exception
Throw e
End Try
End Get
Set(ByVal Value As String)
Try
_BaseUriStaging = Trim(Value)
Catch e As Exception
Throw e
End Try
End Set
End Property
''' <summary>
''' Gets or sets the base file path to be used for file access in production
mode.
''' </summary>
Public Property BaseUriProduction() As String
Get
Try
Return _BaseUriProduction
Catch e As Exception
Throw e
End Try
End Get
Set(ByVal Value As String)
Try
_BaseUriProduction = Trim(Value)
Catch e As Exception
Throw e
End Try
End Set
End Property
''' <summary>
''' Gets the applicaton's base file path based on the
ApplicationEnvironment.
''' </summary>
Public ReadOnly Property ApplicationFilePath() As String
Get
Dim PathFound As String = ""
Select Case _ApplicationEnvironment
Case EnvironmentValues.Development
PathFound = _BaseFilePathDevelopment
Case EnvironmentValues.LocalHost
PathFound = _BaseFilePathLocalhost
Case EnvironmentValues.Production
PathFound = _BaseFilePathProduction
Case EnvironmentValues.Staging
PathFound = _BaseFilePathStaging
End Select
If PathFound.Length = 0 Then
Throw New Exception("The " & _ApplicationEnvironment.ToString & " file path
in the Fortunate.Application object has not been set.")
End If
Return PathFound
End Get
End Property
''' <summary>
''' Gets the applicaton's base uri based on the ApplicationEnvironment.
''' </summary>
Public ReadOnly Property ApplicationUri() As String
Get
Dim UriFound As String = ""
Select Case _ApplicationEnvironment
Case EnvironmentValues.Development
UriFound = _BaseUriDevelopment
Case EnvironmentValues.LocalHost
UriFound = _BaseUriLocalHost
Case EnvironmentValues.Production
UriFound = _BaseUriProduction
Case EnvironmentValues.Staging
UriFound = _BaseUriStaging
End Select
If UriFound.Length = 0 Then
Throw New Exception("The " & _ApplicationEnvironment.ToString & " base uri
in the Fortunate.Application object has not been set.")
End If
Return UriFound
End Get
End Property
#End Region
End Class
Public Class PageUtilities
Inherits System.ComponentModel.Component
#Region "Global Private Variables"
Private _ApplicationObject As Fortunate.Application
#End Region
#Region "Initialize"
Public Sub New(ByVal appObject As Fortunate.Application)
Try
_ApplicationObject = appObject
Catch ex As Exception
Throw ex
End Try
End Sub
#End Region
#Region "Public Functions"
Public Function Uri(ByVal RelativeUri As String, Optional ByVal ReturnSecure
As Boolean = False) As Uri
Try
Dim UriBuilt As Uri
Select Case _ApplicationObject.ApplicationEnvironment
Case Fortunate.Application.EnvironmentValues.Development
UriBuilt = CreateUri(_ApplicationObject.BaseUriDevelopment, RelativeUri,
ReturnSecure)
Case Fortunate.Application.EnvironmentValues.LocalHost
UriBuilt = CreateUri(_ApplicationObject.BaseUriLocalHost, RelativeUri,
ReturnSecure)
Case Fortunate.Application.EnvironmentValues.Production
UriBuilt = CreateUri(_ApplicationObject.BaseUriProduction, RelativeUri,
ReturnSecure)
Case Fortunate.Application.EnvironmentValues.Staging
UriBuilt = CreateUri(_ApplicationObject.BaseUriStaging, RelativeUri,
ReturnSecure)
Case Else
UriBuilt = Nothing
End Select
Return UriBuilt
Catch e As Exception
Throw e
End Try
End Function
Public Function CheckUriStatus(ByVal UriToCheck As Uri, Optional ByVal
Authorization As Net.CredentialCache = Nothing, Optional ByVal ReturnError
As Boolean = False) As Boolean
Try
Dim mwRequest As Net.HttpWebRequest =
CType(Net.WebRequest.Create(UriToCheck), Net.HttpWebRequest)
mwRequest.Credentials = Authorization
Dim mwResponse As Net.HttpWebResponse = CType(mwRequest.GetResponse(),
Net.HttpWebResponse)
Dim Contacted As Boolean
If mwResponse.StatusCode <> Net.HttpStatusCode.OK Then
Contacted = False
Else
Contacted = True
End If
mwResponse.Close()
mwResponse = Nothing
Return Contacted
Catch e As Exception
If ReturnError Then
Throw e
Else
Return False
End If
End Try
End Function
Public Function CheckUriStatus(ByVal UriToCheck As String, Optional ByVal
Authorization As Net.CredentialCache = Nothing, Optional ByVal ReturnError
As Boolean = False) As Boolean
Try
Dim mwRequest As Net.HttpWebRequest =
CType(Net.WebRequest.Create(UriToCheck), Net.HttpWebRequest)
mwRequest.Credentials = Authorization
Dim mwResponse As Net.HttpWebResponse = CType(mwRequest.GetResponse(),
Net.HttpWebResponse)
Dim Contacted As Boolean
If mwResponse.StatusCode <> Net.HttpStatusCode.OK Then
Contacted = False
Else
Contacted = True
End If
mwResponse.Close()
mwResponse = Nothing
Return Contacted
Catch e As Exception
If ReturnError Then
Throw e
Else
Return False
End If
End Try
End Function
#End Region
#Region "Private Utility Functions"
Private Function CreateUri(ByVal BaseUri As String, ByVal RelativeUri As
String, ByVal ReturnSecure As Boolean) As Uri
Try
BaseUri = BaseUri.Replace("https://", "")
BaseUri = BaseUri.Replace("http://", "")
Dim mstrIncludedRelativeUri As String = ""
If BaseUri.IndexOf("/") > -1 Then
Dim StringUtilities1 As New Fortunate.StringUtilities
mstrIncludedRelativeUri = StringUtilities1.ExtractString(BaseUri, "/",
vbCrLf).ToString
BaseUri = BaseUri.Replace(mstrIncludedRelativeUri, "")
End If
If ReturnSecure Then
BaseUri = "https://" & BaseUri
Else
BaseUri = "http://" & BaseUri
End If
Return New Uri(New Uri(BaseUri), mstrIncludedRelativeUri & RelativeUri)
Catch e As Exception
Throw e
End Try
End Function
#End Region
End Class
Sincerely,
--
S. Justin Gengo
Web Developer / Programmer
Free code library:
http://www.aboutfortunate.com
"Out of chaos comes order."
Nietzsche