Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Absolute URL from Relative path (refactored method)

I am really surprised that there is no native .NET method to get an absolute url from a relative url. I know this has been discussed many times, but never have come across a satisfactory method that handles this well. Can you help fine tune the method below?

I think all I need left is to auto choose the protocol instead of hard coding it (http/https). Anything else I am missing (caveats, performance, etc)?

public static string GetAbsoluteUrl(string url)     {         //VALIDATE INPUT FOR ALREADY ABSOLUTE URL         if (url.StartsWith("http://", StringComparison.OrdinalIgnoreCase)             || url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))         {              return url;         }          //GET PAGE REFERENCE FOR CONTEXT PROCESSING         Page page = HttpContext.Current.Handler as Page;          //RESOLVE PATH FOR APPLICATION BEFORE PROCESSING         if (url.StartsWith("~/"))         {             url = page.ResolveUrl(url);         }          //BUILD AND RETURN ABSOLUTE URL         return "http://" + page.Request.ServerVariables["SERVER_NAME"] + "/"                           + url.TrimStart('/');     } 
like image 635
TruMan1 Avatar asked Sep 09 '10 22:09

TruMan1


2 Answers

This has always been my approach to this little nuisance. Note the use of VirtualPathUtility.ToAbsolute(relativeUrl) allows the method to be declared as an extension in a static class.

/// <summary> /// Converts the provided app-relative path into an absolute Url containing the  /// full host name /// </summary> /// <param name="relativeUrl">App-Relative path</param> /// <returns>Provided relativeUrl parameter as fully qualified Url</returns> /// <example>~/path/to/foo to http://www.web.com/path/to/foo</example> public static string ToAbsoluteUrl(this string relativeUrl) {     if (string.IsNullOrEmpty(relativeUrl))         return relativeUrl;      if (HttpContext.Current == null)         return relativeUrl;      if (relativeUrl.StartsWith("/"))         relativeUrl = relativeUrl.Insert(0, "~");     if (!relativeUrl.StartsWith("~/"))         relativeUrl = relativeUrl.Insert(0, "~/");      var url = HttpContext.Current.Request.Url;     var port = url.Port != 80 ? (":" + url.Port) : String.Empty;      return String.Format("{0}://{1}{2}{3}",         url.Scheme, url.Host, port, VirtualPathUtility.ToAbsolute(relativeUrl)); } 
like image 177
2 revs, 2 users 95% Avatar answered Sep 29 '22 02:09

2 revs, 2 users 95%


new System.Uri(Page.Request.Url, "/myRelativeUrl.aspx").AbsoluteUri 
like image 36
g.breeze Avatar answered Sep 29 '22 01:09

g.breeze