The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
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 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.Mvc.Routing;
  4. using System;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. using System.Web;
  8. namespace Teknik.Utilities
  9. {
  10. public static class UrlExtensions
  11. {
  12. public static string SubRouteUrl(this IUrlHelper url, string sub, string routeName)
  13. {
  14. return url.SubRouteUrl(sub, routeName, null, string.Empty);
  15. }
  16. public static string SubRouteUrl(this IUrlHelper url, string sub, string routeName, string hostOverride)
  17. {
  18. return url.SubRouteUrl(sub, routeName, null, hostOverride);
  19. }
  20. public static string SubRouteUrl(this IUrlHelper url, string sub, string routeName, object routeValues)
  21. {
  22. return url.SubRouteUrl(sub, routeName, routeValues, string.Empty);
  23. }
  24. /// <summary>
  25. /// Generates a full URL given the specified sub domain and route name
  26. /// </summary>
  27. /// <param name="url"></param>
  28. /// <param name="sub"></param>
  29. /// <param name="routeName"></param>
  30. /// <param name="routeValues"></param>
  31. /// <returns></returns>
  32. public static string SubRouteUrl(this IUrlHelper url, string sub, string routeName, object routeValues, string hostOverride)
  33. {
  34. string host = url.ActionContext.HttpContext.Request.Host.Value;
  35. string domain = host.GetDomain();
  36. string rightUrl = string.Empty;
  37. // get current subdomain
  38. string curSub = host.GetSubdomain();
  39. // Grab the sub from parameters if it exists
  40. string subParam = url.ActionContext.HttpContext.Request.Query["sub"]; // A subdomain specified as a query parameter takes precedence over the hostname unless on dev
  41. // If the param is not being used, we will use the curSub
  42. if (string.IsNullOrEmpty(subParam))
  43. {
  44. if (url.ActionContext.HttpContext.Request.IsLocal())
  45. {
  46. subParam = sub;
  47. }
  48. else
  49. {
  50. // If we are on dev and no subparam, we need to set the subparam to the specified sub
  51. subParam = (curSub == "dev") ? sub : string.Empty;
  52. string firstSub = (curSub == "dev") ? "dev" : sub;
  53. if (!string.IsNullOrEmpty(firstSub))
  54. {
  55. domain = firstSub + "." + domain;
  56. }
  57. }
  58. }
  59. else
  60. {
  61. string firstSub = (curSub == "dev") ? "dev" : curSub;
  62. if (!string.IsNullOrEmpty(firstSub))
  63. {
  64. domain = firstSub + "." + domain;
  65. }
  66. else
  67. {
  68. domain = host;
  69. }
  70. }
  71. try
  72. {
  73. rightUrl = url.RouteUrl(new UrlRouteContext() { RouteName = routeName, Values = routeValues });
  74. }
  75. catch (ArgumentException ex)
  76. {
  77. }
  78. string fullHost = string.Format("{0}://{1}", url.ActionContext.HttpContext.Request.Scheme, domain);
  79. if (!string.IsNullOrEmpty(hostOverride))
  80. {
  81. fullHost = hostOverride.TrimEnd('/');
  82. }
  83. string absoluteAction = string.Format("{0}{1}", fullHost, rightUrl);
  84. if (!string.IsNullOrEmpty(subParam))
  85. {
  86. absoluteAction = absoluteAction.SetUrlParameter("sub", sub);
  87. }
  88. return absoluteAction;
  89. }
  90. public static string GetUrlParameters(this string url)
  91. {
  92. Uri uri = new Uri(url);
  93. var queryParts = HttpUtility.ParseQueryString(uri.Query);
  94. return queryParts.ToString();
  95. }
  96. public static string SetUrlParameter(this string url, string paramName, string value)
  97. {
  98. return new Uri(url).SetParameter(paramName, value).ToString();
  99. }
  100. public static Uri SetParameter(this Uri url, string paramName, string value)
  101. {
  102. var queryParts = HttpUtility.ParseQueryString(url.Query);
  103. queryParts[paramName] = value;
  104. return new Uri(url.AbsoluteUriExcludingQuery() + '?' + queryParts.ToString());
  105. }
  106. public static string AbsoluteUriExcludingQuery(this Uri url)
  107. {
  108. return url.AbsoluteUri.Split('?').FirstOrDefault() ?? String.Empty;
  109. }
  110. public static string GetSubdomain(this string host)
  111. {
  112. if (host.IndexOf(":") >= 0)
  113. host = host.Substring(0, host.IndexOf(":"));
  114. Regex tldRegex = new Regex(@"\.[a-z]{2,3}\.[a-z]{2}$");
  115. host = tldRegex.Replace(host, "");
  116. tldRegex = new Regex(@"\.[a-z]{2,4}$");
  117. host = tldRegex.Replace(host, "");
  118. if (host.Split('.').Length > 1)
  119. return host.Substring(0, host.IndexOf("."));
  120. else
  121. return string.Empty;
  122. }
  123. public static string GetSubdomain(this IUrlHelper url)
  124. {
  125. string host = url.ActionContext.HttpContext.Request.Host.Value;
  126. // Grab the sub from parameters if it exists
  127. string subParam = url.ActionContext.HttpContext.Request.Query["sub"]; // A subdomain specified as a query parameter takes precedence over the hostname unless on dev
  128. if (string.IsNullOrEmpty(subParam))
  129. {
  130. // If we are on dev and no subparam, we need to set the subparam to the specified sub
  131. subParam = host.GetSubdomain();
  132. }
  133. return subParam;
  134. }
  135. public static string GetDomain(this string host)
  136. {
  137. string domain = host;
  138. var split = host.Split('.'); // split the host by '.'
  139. if (split.Count() > 2)
  140. {
  141. int index = host.IndexOf('.') + 1;
  142. if (index >= 0 && index < host.Length)
  143. domain = host.Substring(index, (host.Length - index));
  144. }
  145. return domain;
  146. }
  147. public static string GetDomain(this Uri uri)
  148. {
  149. string domain = uri.Host;
  150. var split = uri.Host.Split('.'); // split the host by '.'
  151. if (split.Count() > 2)
  152. {
  153. int index = uri.Host.IndexOf('.') + 1;
  154. if (index >= 0 && index < uri.Host.Length)
  155. domain = uri.Host.Substring(index, (uri.Host.Length - index));
  156. }
  157. return domain;
  158. }
  159. public static string GetActive(this IUrlHelper url, params string[] subs)
  160. {
  161. return url.GetActive(null, null, subs);
  162. }
  163. public static string GetActive(this IUrlHelper url, string controller)
  164. {
  165. return url.GetActive(controller, null);
  166. }
  167. public static string GetActive(this IUrlHelper url, string controller, string action, params string[] subs)
  168. {
  169. var curSub = url.GetSubdomain();
  170. var curController = url.ActionContext.RouteData.Values["Controller"]?.ToString();
  171. var curAction = url.ActionContext.RouteData.Values["Action"]?.ToString();
  172. foreach (string sub in subs)
  173. {
  174. if (curSub == sub)
  175. {
  176. if ((string.IsNullOrEmpty(controller) || curController == controller) &&
  177. (string.IsNullOrEmpty(action) || curAction == action))
  178. return "active";
  179. }
  180. }
  181. if (!subs.Any() &&
  182. (string.IsNullOrEmpty(controller) || curController == controller) &&
  183. (string.IsNullOrEmpty(action) || curAction == action))
  184. {
  185. return "active";
  186. }
  187. return string.Empty;
  188. }
  189. public static bool IsValidUrl(this string url)
  190. {
  191. Uri uriResult;
  192. bool result = Uri.TryCreate(url, UriKind.Absolute, out uriResult);
  193. if (result)
  194. {
  195. result = uriResult.IsWellFormedOriginalString();
  196. }
  197. return result;
  198. }
  199. public static string FullURL(this IUrlHelper helper, string virtualPath)
  200. {
  201. var url = string.Format("{0}://{1}{2}", helper.ActionContext.HttpContext.Request.Scheme, helper.ActionContext.HttpContext.Request.Host.ToUriComponent(), helper.Content(virtualPath));
  202. return url;
  203. }
  204. }
  205. }