How do I turn a relative URL into a full URL?
You just need to create a new URI using the page.request.url
and then get the AbsoluteUri
of that:
New System.Uri(Page.Request.Url, "Foo.aspx").AbsoluteUri
Have a play with this (modified from here)
public string ConvertRelativeUrlToAbsoluteUrl(string relativeUrl) {
return string.Format("http{0}://{1}{2}",
(Request.IsSecureConnection) ? "s" : "",
Request.Url.Host,
Page.ResolveUrl(relativeUrl)
);
}
This one's been beat to death but I thought I'd post my own solution which I think is cleaner than many of the other answers.
public static string AbsoluteAction(this UrlHelper url, string actionName, string controllerName, object routeValues)
{
return url.Action(actionName, controllerName, routeValues, url.RequestContext.HttpContext.Request.Url.Scheme);
}
public static string AbsoluteContent(this UrlHelper url, string path)
{
Uri uri = new Uri(path, UriKind.RelativeOrAbsolute);
//If the URI is not already absolute, rebuild it based on the current request.
if (!uri.IsAbsoluteUri)
{
Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
UriBuilder builder = new UriBuilder(requestUrl.Scheme, requestUrl.Host, requestUrl.Port);
builder.Path = VirtualPathUtility.ToAbsolute(path);
uri = builder.Uri;
}
return uri.ToString();
}