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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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;
  30. string rightUrl = string.Empty;
  31. // get current subdomain
  32. string curSub = host.GetSubdomain();
  33. var split = host.Split('.'); // split the host by '.'
  34. if (split.Count() > 2)
  35. {
  36. int index = host.IndexOf('.') + 1;
  37. if (index >= 0 && index < host.Length)
  38. domain = host.Substring(index, (host.Length - index));
  39. }
  40. // Grab the sub from parameters if it exists
  41. string subParam = url.RequestContext.HttpContext.Request.QueryString["sub"]; // A subdomain specified as a query parameter takes precedence over the hostname.
  42. // If the param is not being used, we will use the curSub
  43. if (string.IsNullOrEmpty(subParam))
  44. {
  45. string firstSub = (curSub == "dev") ? "dev" : sub;
  46. if (!string.IsNullOrEmpty(firstSub))
  47. {
  48. routeName = firstSub + "." + routeName;
  49. domain = firstSub + "." + domain;
  50. }
  51. }
  52. else
  53. {
  54. string desiredSub = (subParam == "dev") ? "dev" : sub;
  55. routeName = desiredSub + "." + routeName;
  56. domain = host;
  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) && subParam != "dev")
  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. }
  106. }