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:
+ filename +
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.