Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
My Favorite Little Function - FixUrl
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
Filed under:

[Advertisement]

Comments

Eric Wise wrote re: My Favorite Little Function - FixUrl
on 10-08-2006 12:23 AM
Excellent. I think all of us career ASP .NET types has a function like this floating around in our utilities pile. =)
Jim Bolla wrote re: My Favorite Little Function - FixUrl
on 10-08-2006 11:13 PM

The System.Web.UI.Control class has a method called ResolveClientUrl() that does the same thing. Looking at the source code through Reflector shows its implementation to be much more complicated than yours. Plus it requires a Control instance. Here's my C# implementation of the same idea:

public static string ResolveRelativeUrl(string relativeUrl)

{

if (!relativeUrl.StartsWith("~/"))

return relativeUrl;

else

{

string currentApplicationPath = HttpContext.Current.Request.ApplicationPath;

string relativeUrlWithNoLeadingTildeSlash = relativeUrl.Remove(0, 2);

return currentApplicationPath + relativeUrlWithNoLeadingTildeSlash;

}

}

Zach Parrish wrote re: My Favorite Little Function - FixUrl
on 10-09-2006 9:25 AM
ResolveClientUrl is probably more complicated because it doesn't do the exactly same thing as what's posted here. I always just use ResolveUrl or ResolveClientUrl. They already exist in the framework and while you do need a Control instance, if you have the Page object handy then that'll do just fine. ResolveClientUrl will give you a relative path depending on what page you're on (i.e. "../../test/test.aspx"), while ResolveUrl will give you an absolute path from the root of the app (i.e. "/root/test/test.aspx").
James Curran wrote re: My Favorite Little Function - FixUrl
on 10-11-2006 10:08 AM
I'm just bothered by doing the StartsWith twice for no reason. Here's the revised code:

Public Function FixUrl(ByVal Url As String) As String

            Dim strReturn As String

            If Url.StartsWith("~") Then

                strReturn = Url.Substring(1)

                If HttpContext.Current.Request.ApplicationPath <> "/" Then

                    strReturn = HttpContext.Current.Request.ApplicationPath & strReturn.Replace("//", "/")

                End If

            Else

                strReturn = Url

            End If

            Return strReturn

        End Function

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)