One problem that I frequently run into is trying to get my javascript URLs to point to the correct directory by using the "~" root for an ASP.NET application. Here's a handy function that helps with trying to get to the root of a webapp:
Public Function FixUrl(ByVal Url As String) As String
Dim strReturn As String
If Url.StartsWith("~") And HttpContext.Current.Request.ApplicationPath <> "/" Then
strReturn = HttpContext.Current.Request.ApplicationPath & Url.Substring(1).Replace("//", "/")
ElseIf Url.StartsWith("~") Then
strReturn = Right(Url, Len(Url) - 1)
Else
strReturn = Url
End If
Return strReturn
End Function
So what this function will do is take any URL that is passed (such as "~/admin/user.aspx") will return the proper directory.
If you are running your application at this URL: http://mytestapp.domain.com/, it will return "/admin/user.aspx". If you are running it locally for testing, such as http://localhost/mytestapp/, it will return "/mytestapp/admin/user.aspx".
This helps out when you are adding URLs to javascript calls through code (such as window.open('/admin/user.aspx', null, 'etc...) and need to have a generic way to find the root of your application because the tilde will not work in this context.
Enjoy!
Posted
10-07-2006 12:45 AM
by
Stephen Wright