ASP.NET: Base Url and Absolute Url

Seems like I have to dig everytime I need an absolute url.

From :

public static Uri GetBaseUrl(HttpRequest request)
    if (request == null || request.Url == null || request.ApplicationPath == null)
        throw new ArgumentException("Cannot be null", "request");

    Uri contextUri = new Uri(request.Url, request.RawUrl);
    UriBuilder realmUri = new UriBuilder(contextUri) { Path = request.ApplicationPath, Query = null, Fragment = null };
    return realmUri.Uri;

public static string GetAbsoluteUrl(HttpRequest request, string relativeUrl)
    return new Uri(GetBaseUrl(request), VirtualPathUtility.ToAbsolute(relativeUrl)).AbsoluteUri;

Other links:

The End.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: