How do I turn a relative URL into a full URL?

Solution 1:

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)
    );
}

Solution 2:

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();
}

Solution 3:

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

Solution 4:

This is my helper function to do this

public string GetFullUrl(string relativeUrl) {
    string root = Request.Url.GetLeftPart(UriPartial.Authority);
    return root + Page.ResolveUrl("~/" + relativeUrl) ;
}