Posted By

plugables on 09/09/11


Tagged

url aspnet relative absolute


Versions (?)

static ResolveUrl and ResolveServerUrl


 / Published in: C#
 

URL: http://www.west-wind.com/weblog/posts/2007/Sep/18/ResolveUrl-without-Page

You do not have Page object available everywhere to use Page.ResolveUrl. Here is an alternate.

  1. /// <summary>
  2. /// Returns a site relative HTTP path from a partial path starting out with a ~.
  3. /// Same syntax that ASP.Net internally supports but this method can be used
  4. /// outside of the Page framework.
  5. ///
  6. /// Works like Control.ResolveUrl including support for ~ syntax
  7. /// but returns an absolute URL.
  8. /// </summary>
  9. /// <param name="originalUrl">Any Url including those starting with ~</param>
  10. /// <returns>relative url</returns>
  11. public static string ResolveUrl(string originalUrl)
  12. {
  13. if (string.IsNullOrEmpty(originalUrl))
  14. return originalUrl;
  15.  
  16. // *** Absolute path - just return
  17. if (IsAbsolutePath(originalUrl))
  18. return originalUrl;
  19.  
  20. // *** We don't start with the '~' -> we don't process the Url
  21. if (!originalUrl.StartsWith("~"))
  22. return originalUrl;
  23.  
  24. // *** Fix up path for ~ root app dir directory
  25. // VirtualPathUtility blows up if there is a
  26. // query string, so we have to account for this.
  27. int queryStringStartIndex = originalUrl.IndexOf('?');
  28. if (queryStringStartIndex != -1)
  29. {
  30. string queryString = originalUrl.Substring(queryStringStartIndex);
  31. string baseUrl = originalUrl.Substring(0, queryStringStartIndex);
  32.  
  33. return string.Concat(
  34. VirtualPathUtility.ToAbsolute(baseUrl),
  35. queryString);
  36. }
  37. else
  38. {
  39. return VirtualPathUtility.ToAbsolute(originalUrl);
  40. }
  41.  
  42. }
  43.  
  44. /// <summary>
  45. /// This method returns a fully qualified absolute server Url which includes
  46. /// the protocol, server, port in addition to the server relative Url.
  47. ///
  48. /// Works like Control.ResolveUrl including support for ~ syntax
  49. /// but returns an absolute URL.
  50. /// </summary>
  51. /// <param name="ServerUrl">Any Url, either App relative or fully qualified</param>
  52. /// <param name="forceHttps">if true forces the url to use https</param>
  53. /// <returns></returns>
  54. public static string ResolveServerUrl(string serverUrl, bool forceHttps)
  55. {
  56. if (string.IsNullOrEmpty(serverUrl))
  57. return serverUrl;
  58.  
  59. // *** Is it already an absolute Url?
  60. if(IsAbsolutePath(serverUrl))
  61. return serverUrl;
  62.  
  63. string newServerUrl = ResolveUrl(serverUrl);
  64. Uri result = new Uri(HttpContext.Current.Request.Url, newServerUrl);
  65.  
  66. if (!forceHttps)
  67. return result.ToString();
  68. else
  69. return ForceUriToHttps(result).ToString();
  70. }
  71.  
  72. /// <summary>
  73. /// This method returns a fully qualified absolute server Url which includes
  74. /// the protocol, server, port in addition to the server relative Url.
  75. ///
  76. /// It work like Page.ResolveUrl, but adds these to the beginning.
  77. /// This method is useful for generating Urls for AJAX methods
  78. /// </summary>
  79. /// <param name="ServerUrl">Any Url, either App relative or fully qualified</param>
  80. /// <returns></returns>
  81. public static string ResolveServerUrl(string serverUrl)
  82. {
  83. return ResolveServerUrl(serverUrl, false);
  84. }
  85.  
  86. /// <summary>
  87. /// Forces the Uri to use https
  88. /// </summary>
  89. private static Uri ForceUriToHttps(Uri uri)
  90. {
  91. // ** Re-write Url using builder.
  92. UriBuilder builder = new UriBuilder(uri);
  93. builder.Scheme = Uri.UriSchemeHttps;
  94. builder.Port = 443;
  95.  
  96. return builder.Uri;
  97. }
  98.  
  99. private static bool IsAbsolutePath(string originalUrl)
  100. {
  101. // *** Absolute path - just return
  102. int IndexOfSlashes = originalUrl.IndexOf("://");
  103. int IndexOfQuestionMarks = originalUrl.IndexOf("?");
  104.  
  105. if (IndexOfSlashes > -1 &&
  106. (IndexOfQuestionMarks < 0 ||
  107. (IndexOfQuestionMarks > -1 && IndexOfQuestionMarks > IndexOfSlashes)
  108. )
  109. )
  110. return true;
  111.  
  112. return false;
  113. }

Report this snippet  

You need to login to post a comment.