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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.Helpers;
  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. string curSub = host.GetSubdomain();
  45. string domain = host.GetDomain();
  46. // special consideration for 'dev' subdomain
  47. if (subdomain == null || subdomain == "dev")
  48. {
  49. if (!string.IsNullOrEmpty(curSub) && curSub == "dev")
  50. {
  51. // if we are on dev, and the param is dev or empty, we need to initialize it to 'www'
  52. subdomain = "www";
  53. }
  54. }
  55. if (subdomain == null)
  56. {
  57. subdomain = curSub;
  58. }
  59. else
  60. {
  61. if (routeData.Values["sub"] == null)
  62. {
  63. routeData.Values["sub"] = subdomain;
  64. }
  65. else
  66. {
  67. subdomain = routeData.Values["sub"].ToString();
  68. }
  69. }
  70. // Check if this route is valid for the current domain
  71. if (httpContext.Request.IsLocal || Domains.Contains(domain))
  72. {
  73. // Check if this route is valid for the current subdomain ('*' means any subdomain is valid)
  74. if (Subdomains.Contains("*") || Subdomains.Contains(subdomain))
  75. {
  76. routeData.Values["sub"] = subdomain;
  77. return routeData;
  78. }
  79. }
  80. return null;
  81. }
  82. public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
  83. {
  84. object subdomainParam = requestContext.HttpContext.Request.QueryString["sub"];
  85. if (subdomainParam != null && values["sub"] == null)
  86. values["sub"] = subdomainParam;
  87. return base.GetVirtualPath(requestContext, values); // we now have the route based on subdomain
  88. }
  89. }
  90. }