Welcome to Professional ASP.NET - Chris Love's Official Blog Sign in | Join | Help

Chris Love's Official ASP.NET Blog

Chris Love's Helpful tips, tricks and pragmatic development knowledge for the ASP.NET world.
Add to Technorati Favorites


ASP Insider
Hidden Utility Class - URLPath

I remember when I was in college and I found a book called 'Windows Exposed' or something implying that there was a lot of hidden gems in Windows to program against that just were not documented.  That trend continues through .NET 2.0 (now 3.0 I guess is officially where we are).  I often create utility classes to perform common routines, conversion and grunt work for me.  Typically a few days or weeks later I eventually find there is a base class/method that perfoms that task for me.  This week I found URLPath while I was trying to do some tricks analyzing URL data returned by a web service.

URLPath for the most part is an extension of the Path class with methods created to handle the extra things that are invovled for a valid URL path.  For example GetFileName is just a wrapper to Path.GetFileName.  AppendSlashToPathIfNeeded is a little routine to ad a trailing forward slash (whack) to a path it if does not have one, a URL path issue.

For some of the things I have been into lately this little class is a gem of a find for me.

Friend MustInherit NotInheritable Class UrlPath
      ' Methods
      Shared Sub New()
      Friend Shared Function AppendSlashToPathIfNeeded(ByVal path As String) As String
      Friend Shared Sub CheckValidVirtualPath(ByVal path As String)
      Friend Shared Function Combine(ByVal basepath As String, ByVal relative As String) As String
      Private Shared Function Combine(ByVal appPath As String, ByVal basepath As String, ByVal relative As String) As String
      Friend Shared Function FixVirtualPathSlashes(ByVal virtualPath As String) As String
      Friend Shared Function GetDirectory(ByVal path As String) As String
      Friend Shared Function GetDirectoryOrRootName(ByVal path As String) As String
      Friend Shared Function GetExtension(ByVal virtualPath As String) As String
      Friend Shared Function GetFileName(ByVal virtualPath As String) As String
      Friend Shared Function GetFileNameWithoutExtension(ByVal virtualPath As String) As String
      Friend Shared Function HasTrailingSlash(ByVal virtualPath As String) As Boolean
      Friend Shared Function IsAbsolutePhysicalPath(ByVal path As String) As Boolean
      Friend Shared Function IsAppRelativePath(ByVal path As String) As Boolean
      Private Shared Function IsDirectorySeparatorChar(ByVal ch As Char) As Boolean
      Friend Shared Function IsEqualOrSubpath(ByVal path As String, ByVal subpath As String) As Boolean
      Friend Shared Function IsPathOnSameServer(ByVal absUriOrLocalPath As String, ByVal currentRequestUri As Uri) As Boolean
      Friend Shared Function IsRelativeUrl(ByVal virtualPath As String) As Boolean
      Friend Shared Function IsRooted(ByVal basepath As String) As Boolean
      Friend Shared Function IsUncSharePath(ByVal path As String) As Boolean
      Friend Shared Function IsValidVirtualPathWithoutProtocol(ByVal path As String) As Boolean
      Friend Shared Function MakeRelative(ByVal from As String, ByVal [to] As String) As String
      Friend Shared Function MakeVirtualPathAppAbsolute(ByVal virtualPath As String) As String
      Friend Shared Function MakeVirtualPathAppAbsolute(ByVal virtualPath As String, ByVal applicationPath As String) As String
      Friend Shared Function MakeVirtualPathAppAbsoluteReduceAndCheck(ByVal virtualPath As String) As String
      Friend Shared Function MakeVirtualPathAppRelative(ByVal virtualPath As String) As String
      Friend Shared Function MakeVirtualPathAppRelative(ByVal virtualPath As String, ByVal applicationPath As String, ByVal nullIfNotInApp As Boolean) As String
      Friend Shared Function MakeVirtualPathAppRelativeOrNull(ByVal virtualPath As String) As String
      Friend Shared Function PathEndsWithExtraSlash(ByVal path As String) As Boolean
      Friend Shared Function PathIsDriveRoot(ByVal path As String) As Boolean
      Friend Shared Function Reduce(ByVal path As String) As String
      Friend Shared Function ReduceVirtualPath(ByVal path As String) As String
      Friend Shared Function RemoveSlashFromPathIfNeeded(ByVal path As String) As String
      Friend Shared Function SimpleCombine(ByVal basepath As String, ByVal relative As String) As String
      Friend Shared Function VirtualPathStartsWithAppPath(ByVal virtualPath As String) As Boolean
      Private Shared Function VirtualPathStartsWithVirtualPath(ByVal virtualPath1 As String, ByVal virtualPath2 As String) As Boolean

      ' Fields
      Friend Const appRelativeCharacter As Char = "~"c
      Friend Const appRelativeCharacterString As String = "~/"
      Private Const dummyProtocolAndServer As String = "file://foo"
      Private Shared s_slashChars As Char()
End Class

Posted: Thursday, November 23, 2006 10:10 AM

by Chris Love

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS