The next generation of the Teknik Services. Written in ASP.NET. Fork for blog tags.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UrlExtensions.cs 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using System.Web.Routing;
  9. using System.Web.UI;
  10. namespace Teknik
  11. {
  12. public static class UrlExtensions
  13. {
  14. public static string SubRouteUrl(this UrlHelper url, string sub, string routeName)
  15. {
  16. return url.SubRouteUrl(sub, routeName, null);
  17. }
  18. /// <summary>
  19. /// Generates a full URL given the specified sub domain and route name
  20. /// </summary>
  21. /// <param name="url"></param>
  22. /// <param name="sub"></param>
  23. /// <param name="routeName"></param>
  24. /// <param name="routeValues"></param>
  25. /// <returns></returns>
  26. public static string SubRouteUrl(this UrlHelper url, string sub, string routeName, object routeValues)
  27. {
  28. string host = url.RequestContext.HttpContext.Request.Url.Authority;
  29. string domain = host.GetDomain();
  30. string rightUrl = string.Empty;
  31. // get current subdomain
  32. string curSub = host.GetSubdomain();
  33. // Grab the sub from parameters if it exists
  34. string subParam = url.RequestContext.HttpContext.Request.QueryString["sub"]; // A subdomain specified as a query parameter takes precedence over the hostname unless on dev
  35. // If the param is not being used, we will use the curSub
  36. if (string.IsNullOrEmpty(subParam))
  37. {
  38. // If we are on dev and no subparam, we need to set the subparam to the specified sub
  39. subParam = (curSub == "dev") ? sub : string.Empty;
  40. string firstSub = (curSub == "dev") ? "dev" : sub;
  41. if (!string.IsNullOrEmpty(firstSub))
  42. {
  43. domain = firstSub + "." + domain;
  44. }
  45. }
  46. else
  47. {
  48. string firstSub = (curSub == "dev") ? "dev" : curSub;
  49. if (!string.IsNullOrEmpty(firstSub))
  50. {
  51. domain = firstSub + "." + domain;
  52. }
  53. else
  54. {
  55. domain = host;
  56. }
  57. }
  58. try
  59. {
  60. rightUrl = url.RouteUrl(routeName, routeValues);
  61. }
  62. catch (ArgumentException)
  63. {
  64. }
  65. string absoluteAction = string.Format("{0}://{1}{2}", url.RequestContext.HttpContext.Request.Url.Scheme, domain, rightUrl);
  66. if (!string.IsNullOrEmpty(subParam))
  67. {
  68. absoluteAction = absoluteAction.SetUrlParameter("sub", sub);
  69. }
  70. return absoluteAction;
  71. }
  72. public static string GetUrlParameters(this string url)
  73. {
  74. Uri uri = new Uri(url);
  75. var queryParts = HttpUtility.ParseQueryString(uri.Query);
  76. return queryParts.ToString();
  77. }
  78. public static string SetUrlParameter(this string url, string paramName, string value)
  79. {
  80. return new Uri(url).SetParameter(paramName, value).ToString();
  81. }
  82. public static Uri SetParameter(this Uri url, string paramName, string value)
  83. {
  84. var queryParts = HttpUtility.ParseQueryString(url.Query);
  85. queryParts[paramName] = value;
  86. return new Uri(url.AbsoluteUriExcludingQuery() + '?' + queryParts.ToString());
  87. }
  88. public static string AbsoluteUriExcludingQuery(this Uri url)
  89. {
  90. return url.AbsoluteUri.Split('?').FirstOrDefault() ?? String.Empty;
  91. }
  92. public static string GetSubdomain(this string host)
  93. {
  94. if (host.IndexOf(":") >= 0)
  95. host = host.Substring(0, host.IndexOf(":"));
  96. Regex tldRegex = new Regex(@"\.[a-z]{2,3}\.[a-z]{2}$");
  97. host = tldRegex.Replace(host, "");
  98. tldRegex = new Regex(@"\.[a-z]{2,4}$");
  99. host = tldRegex.Replace(host, "");
  100. if (host.Split('.').Length > 1)
  101. return host.Substring(0, host.IndexOf("."));
  102. else
  103. return string.Empty;
  104. }
  105. public static string GetSubdomain(this UrlHelper url)
  106. {
  107. string host = url.RequestContext.HttpContext.Request.Url.Authority;
  108. // Grab the sub from parameters if it exists
  109. string subParam = url.RequestContext.HttpContext.Request.QueryString["sub"]; // A subdomain specified as a query parameter takes precedence over the hostname unless on dev
  110. if (string.IsNullOrEmpty(subParam))
  111. {
  112. // If we are on dev and no subparam, we need to set the subparam to the specified sub
  113. subParam = host.GetSubdomain();
  114. }
  115. return subParam;
  116. }
  117. public static string GetDomain(this string host)
  118. {
  119. string domain = host;
  120. var split = host.Split('.'); // split the host by '.'
  121. if (split.Count() > 2)
  122. {
  123. int index = host.IndexOf('.') + 1;
  124. if (index >= 0 && index < host.Length)
  125. domain = host.Substring(index, (host.Length - index));
  126. }
  127. return domain;
  128. }
  129. public static string GetDomain(this Uri uri)
  130. {
  131. string domain = uri.Host;
  132. var split = uri.Host.Split('.'); // split the host by '.'
  133. if (split.Count() > 2)
  134. {
  135. int index = uri.Host.IndexOf('.') + 1;
  136. if (index >= 0 && index < uri.Host.Length)
  137. domain = uri.Host.Substring(index, (uri.Host.Length - index));
  138. }
  139. return domain;
  140. }
  141. public static string GetActive(this UrlHelper url, params string[] subs)
  142. {
  143. string curSub = url.GetSubdomain();
  144. foreach (string sub in subs)
  145. {
  146. if (curSub == sub)
  147. {
  148. return "active";
  149. }
  150. }
  151. return string.Empty;
  152. }
  153. public static bool IsValidUrl(this string url)
  154. {
  155. Uri uriResult;
  156. bool result = Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out uriResult);
  157. if (result)
  158. {
  159. result = uriResult.IsWellFormedOriginalString();
  160. }
  161. return result;
  162. }
  163. }
  164. }