Getting an absolute URL from ASP.NET Webforms

ASP.NET No Comments

Related to the post Getting an absolute URL from ASP.NET MVC here is the corresponding ASP.NET Webforms version (not as extension methods):

public static Uri GetBaseUrl(HttpRequest 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;
}

Leave a Reply

Icons by N.Design Studio. Designed By Ben Swift. Powered by WordPress, Search Optimization and Free WordPress Themes
Entries RSS Comments RSS Log in