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.

SubdomainRoute.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Web.Routing;
  7. using Teknik.Utilities;
  8. namespace Teknik
  9. {
  10. public class SubdomainRoute : Route
  11. {
  12. public List<string> Subdomains { get; set; }
  13. public List<string> Domains { get; set; }
  14. public SubdomainRoute(List<string> subdomains, List<string> domains, string url, IRouteHandler handler)
  15. : base(url, handler)
  16. {
  17. this.Subdomains = subdomains;
  18. this.Domains = domains;
  19. }
  20. public SubdomainRoute(List<string> subdomains, List<string> domains, string url, RouteValueDictionary defaults, IRouteHandler handler)
  21. : base(url, defaults, handler)
  22. {
  23. this.Subdomains = subdomains;
  24. this.Domains = domains;
  25. }
  26. public SubdomainRoute(List<string> subdomains, List<string> domains, string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler handler)
  27. : base(url, defaults, constraints, handler)
  28. {
  29. this.Subdomains = subdomains;
  30. this.Domains = domains;
  31. }
  32. public SubdomainRoute(List<string> subdomains, List<string> domains, string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler handler)
  33. : base(url, defaults, constraints, dataTokens, handler)
  34. {
  35. this.Subdomains = subdomains;
  36. this.Domains = domains;
  37. }
  38. public override RouteData GetRouteData(HttpContextBase httpContext)
  39. {
  40. var routeData = base.GetRouteData(httpContext);
  41. if (routeData == null) return null; // Only look at the subdomain if this route matches in the first place.
  42. string subdomain = httpContext.Request.QueryString["sub"]; // A subdomain specified as a query parameter takes precedence over the hostname.
  43. string host = httpContext.Request.Headers["Host"];
  44. if (host == null) return null; // no host, so don't process it
  45. string domain = host.GetDomain();
  46. string curSub = host.GetSubdomain();
  47. // special consideration for 'dev' subdomain
  48. if (subdomain == null || subdomain == "dev")
  49. {
  50. if (!string.IsNullOrEmpty(curSub) && curSub == "dev")
  51. {
  52. // if we are on dev, and the param is dev or empty, we need to initialize it to 'www'
  53. subdomain = "www";
  54. }
  55. }
  56. if (subdomain == null)
  57. {
  58. subdomain = curSub;
  59. }
  60. else
  61. {
  62. if (routeData.Values["sub"] == null)
  63. {
  64. routeData.Values["sub"] = subdomain;
  65. }
  66. else
  67. {
  68. subdomain = routeData.Values["sub"].ToString();
  69. }
  70. }
  71. // Check if this route is valid for the current domain
  72. if (httpContext.Request.IsLocal || Domains.Contains(domain))
  73. {
  74. // Check if this route is valid for the current subdomain ('*' means any subdomain is valid)
  75. if (Subdomains.Contains("*") || Subdomains.Contains(subdomain))
  76. {
  77. routeData.Values["sub"] = subdomain;
  78. return routeData;
  79. }
  80. }
  81. return null;
  82. }
  83. public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
  84. {
  85. object subdomainParam = requestContext.HttpContext.Request.QueryString["sub"];
  86. if (subdomainParam != null && values["sub"] == null)
  87. values["sub"] = subdomainParam;
  88. return base.GetVirtualPath(requestContext, values); // we now have the route based on subdomain
  89. }
  90. }
  91. }