Thursday, February 7, 2008

confusing file path for web developer .net

Are you one of the developer always confuse whith this kind of path e "games/alien.jpg" or "../games/alien.jpg" or "/games/alien.jpg" ? Even if you know the meaning, i am sure you still have problem of this because the path is constructed as it depends how the caller is located. Seem to be over complicated here. :)

I am one of the developer that alwasys confuse with file path that looks like <*img src=".../love/app/basic.jpg" /*>". i really do not like because it can be problematic especially if you are ASP.NET developer that uses master pages across the pages.

if you are one of those guy that always experince broken links, I have an elegant solution to this problem.

Given a virtual directory named site, which can be accessibled as http://localhost/site and you have a file which located at http://localhost/site/games/alien.jpg

to effectively locate the alien.jpg, you got to use this


In this way, you won't be confused how the file is located. ~ represents the parent virtual path.

Ok, if you wish to upload the file to the server using the ResolveUrl don't give you the real path... in this case, replace the ResolveUrl method to MapPath method.


MapPath("~/games/alien.jpg"). The file will be uploaded to the location of '~'.

Got it?

No comments: