CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeffrey Palermo [MVP]

Software management consultant and CTO, Headspring Systems

How VirtualPathUtility combines paths in .Net 2.0 - level 100

This is a warning that the VirtualPathUtility class in .Net 2.0 takes some time to understand the rules under which it operates.  It wasn't obvious to me, and I had to do some investigation to learn it's behavior.  I expected the following test to pass.  It didn't.

        [Test]
        public void ShouldCombineTwoWebsitePaths()
        {
            string parentPath = "/websiteRoot";
            string subDirectory = "newPage";
            string expected = "/websiteRoot/newPage";

            string actual = VirtualPathUtility.Combine(parentPath, subDirectory);
            Assert.AreEqual(expected, actual);
        }


I expected it to actually "Combine" the base path and the path that was relative to the base path.  After all, if these combine, the product will be the sum of both, right?  Not really.  Here's my test output:

    String lengths differ.  Expected length=20, but was length=8.
    Strings differ at index 1.
   
    expected:<"/websiteRoot/newPage">
     but was:<"/newPage">
    ------------^

Thanks to the first comment below, I realized that this method tries to act like a web browser in resolving relative paths.  If I add a "/" to the end of "/websiteRoot", the test would have passed.  As it stands, it assumes that "websiteRoot" is a page and not a directory.



Comments

Narwade said:

plz will u help me in..............

i want to store images in separate virtual diectory & wann to access that at run time

# July 17, 2007 7:08 AM

Justin M. Keyes said:

@ " Doh!": the behavior of VirtualPathUtility.Combine() is counter to the behavior of System.IO.Path.Combine().

# September 6, 2007 8:27 PM

About Jeffrey Palermo

Jeffrey Palermo is a software management consultant and the CTO of Headspring Systems in Austin, TX. Jeffrey specializes in Agile coaching and helps companies double the productivity of software teams. Jeffrey is an MCSD.Net , Microsoft MVP, Certified Scrummaster, Austin .Net User Group leader, AgileAustin board member, INETA speaker, INETA Membership Mentor, Christian, husband, father, motorcyclist, Eagle Scout, U.S. Army Veteran, and Texas A&M University graduate. Check out Devlicio.us!

This Blog

Syndication