Reputation: 12711
My main domain is http://redrocksoftware.com.au. i have an relative path to a file like /PDF/myfile.pdf
I need to convert this relative path into full URL. something like below.
http://redrocksoftware.com.au/PDF/myfile.pdf
i tried below but did not worked
VirtualPathUtility.ToAbsolute("/PDF/myfile.pdf")
Upvotes: 1
Views: 18742
Reputation: 1
Return new System.Uri(Page.Request.Url, ResolveClientUrl("~/relative/path.aspx")).AbsoluteUri
Upvotes: 0
Reputation: 21447
The following extension method worked for me:
public static class Extensions
{
/// <summary>
/// Turns a relative URL into a fully qualified URL.
/// (ie http://domain.com/path?query)
/// </summary>
/// <param name="request"></param>
/// <param name="relativeUrl"></param>
/// <returns></returns>
public static string GetFullUrl(this HttpRequest request, string relativeUrl) {
return String.Format("{0}://{1}{2}",
request.Url.Scheme,
request.Url.Authority,
VirtualPathUtility.ToAbsolute(relativeUrl));
}
}
Use it as follows:
HttpContext.Current.Request.GetFullUrl("~/MyOtherPage.aspx");
Works with virtual directories too, and you dont need MVC.
Upvotes: 8
Reputation: 2216
You can use
string FullUrl = Request.Url.Scheme + System.Uri.SchemeDelimiter + Request.Url.Host + "/PDF/MyFile.pdf"
It works in asp.net, I'm not sure about MVC, but it should work too.
Upvotes: 7