Posts By Category

Posts By Date

Resources:

C# Books
ASP.NET Books DotNet4All








If you like to support this site, feel free to make a donation to support improvements.

Thank you!

Monetize Your Blog

Using HttpRuntime to get the web application absolute path

We all know in ASP.NET, to get the absolute path from the relative path, you can prefix your relative path with the Tilde character as follows:

MyHyperLink.NavigateUrl = "~/Catalog/ASP/Products";

The advantages of the above code over hard coding the absolute path is that you are guaranteed wherever your run your ASP.NET application, it will always find correct absolute path provided that the relative path is navigable on the server. So if your domain name changes for example, or you ship your code over to the customer to install on their server under a new domain name, the above code will still work as long as you maintain the above relative path in the internal directory structure of our web application.

Ok, nothing new here, so what point am I trying to bring? Well, recently, I was working on a freelance project, and instead of assigning the relative path to a Hyperlink Control like in the above, I needed to pass it in a query string to another process. That's when the tilde ~ character simply did not cut it.

So what's another way to get the absolute path from the relative path without using the tilde ~ character?

Simply, use the AppDomainAppVirtualPath property for HttpRuntime, which is an exact equivalent to the tilde ~ character. To do so, the above code now can be replaced with:

MyHyperLink.NavigateUrl = HttpRuntime.AppDomainAppVirtualPath + "/Catalog/ASP/Products";

This made all the difference when I passed the path over as a query string to another process, for example code like this:

lnkPlayUploadedVideo.NavigateUrl =

"~/assets/Player.swf?file=" +  HttpRuntime .AppDomainAppVirtualPath + "/Videos/ConvertedFlv/" + filename + "&bufferTime=3&startAt=0&autoStart=false" ;

Notice that I used the tilde ~ character in the first string to get to my player file, but I used HttpRuntime.AppDomainAppVirtualPathwhen I passed the http path for the file over to the player as an argument parameter. Replacing with tilde ~ character in the argument string won't resolve the absolute path, so I needed to use the HttpRuntime property instead.

Just a tip from a recent project I worked on, thought I share with every one.

kick it on DotNetKicks.com

Feedback

Posted on 9/1/2010 1:13:14 PM

It is great! it worked for me very well.

Posted on 7/15/2010 6:22:38 AM

Of course, for combining strings and path the '+' operator is not recommended.

In this case you should use:

string path = Path.Combine(HttpRuntime.AppDomainAppVirtualPath,@"log4net.config")

or (not quite as good):

string.Format(
"~/assets/Player.swf?file={0}/Videos/ConvertedFlv/{1}"&bufferTime=3&startAt=0&autoStart=false",
HttpRuntime.AppDomainAppVirtualPath,
filename)

Posted on 12/19/2009 1:10:31 PM

Hi,
my drive is d and in this one folder and one file like its absolute path is D:\samir\samir.txt
i want this Relative path please send me on my mail,
samirsavasani@gmail.com

Posted on 5/10/2009 1:37:21 PM

Nice! Thanks!

Posted on 5/13/2008 10:46:38 AM

Thanks,
Steve

Posted on 10/9/2007 1:49:55 PM

You used HttpRuntime.AppDomainAppVirtualPath. However there is other way of resolving relative url's without Control.ResolveUrl - http://dotnettipoftheday.org/tips/VirtualPathUtility-ToAbsolute.aspx

Posted on 6/7/2007 4:27:45 AM

Hi Mads,

Yes, that also works too... Thanks for the tip! I still haven't figured out if there was something in ASP.NET that gets the site's absoulte site path by itself (i.e only the http://www.mycsharpcorner.com part) from within the Web app. The closest thing I came up with was to parse the string returned by Request.Url.AbsoluteUri. Here is my solution:

// Get Site Path
string strUriWithouthttp=Request.Url.AbsoluteUri.Replace("http://", "");
int indexOfSlash = strUriWithouthttp.IndexOf('/');
indexOfSlash = (indexOfSlash == -1 ? strUriWithouthttp.Length : indexOfSlash);
string sitePath = "http://" + strUriWithouthttp.Substring(0, indexOfSlash);

Only thing is that it feels wrong that my web app has to call and parse a property coming from a Request object (Request.Url) to dynamically find out what is my web app's site path.... Do you know of a better way?

Thanks,
Yousef

Posted on 5/30/2007 11:36:41 AM

That's nice. You could also use VirtualPathUtility.ToAbsolute("~/"),

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 


Copyright © 2007 Yousef Mannaa. All material on this site is copyrighted.
Do not publish or reproduce any of this material without written permission from the Author