Get root path in ASP.NET without using Tilde ~

As you probably know you can use the tilde sign (~) in ASP.NET to get the relative path of the root of your Web Site or Web Application. But unless using the Link Server Control or similar you may need some code to convert it to a correct url or get the absolute path.

Using a Server Control – example using the HyperLink Web Server Control
[code lang=”vb”]
MyHyperLink.NavigateUrl = "~/Products/XYZ"
[/code]

Using HttpRuntime.AppDomainAppVirtualPath
[code lang=”vb”]
strAbsoluteUrl = HttpRuntime.AppDomainAppVirtualPath & "/Products/XYZ"
[/code]

Using VirtualPathUtility.ToAbsolute
[code lang=”vb”]
strAbsoluteUrl = VirtualPathUtility.ToAbsolute("~/Products/XYZ")
[/code]

Thanks to Yousef

Leave a Reply

Your email address will not be published. Required fields are marked *