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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. namespace Teknik
  8. {
  9. public class SubdomainRoute : Route
  10. {
  11. public List<string> Subdomains { get; set; }
  12. public SubdomainRoute(List<string> subdomains, string url, IRouteHandler handler)
  13. : base(url, handler)
  14. {
  15. this.Subdomains = subdomains;
  16. }
  17. public SubdomainRoute(List<string> subdomains, string url, RouteValueDictionary defaults, IRouteHandler handler)
  18. : base(url, defaults, handler)
  19. {
  20. this.Subdomains = subdomains;
  21. }
  22. public SubdomainRoute(List<string> subdomains, string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler handler)
  23. : base(url, defaults, constraints, handler)
  24. {
  25. this.Subdomains = subdomains;
  26. }
  27. public SubdomainRoute(List<string> subdomains, string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler handler)
  28. : base(url, defaults, constraints, dataTokens, handler)
  29. {
  30. this.Subdomains = subdomains;
  31. }
  32. public override RouteData GetRouteData(HttpContextBase httpContext)
  33. {
  34. var routeData = base.GetRouteData(httpContext);
  35. if (routeData == null) return null; // Only look at the subdomain if this route matches in the first place.
  36. string subdomain = httpContext.Request.QueryString["sub"]; // A subdomain specified as a query parameter takes precedence over the hostname.
  37. string host = httpContext.Request.Headers["Host"];
  38. string curSub = host.GetSubdomain();
  39. // special consideration for 'dev' subdomain
  40. if (subdomain == null || subdomain == "dev")
  41. {
  42. if (!string.IsNullOrEmpty(curSub) && curSub == "dev")
  43. {
  44. // if we are on dev, and the param is dev or empty, we need to initialize it to 'www'
  45. subdomain = "www";
  46. }
  47. }
  48. if (subdomain == null)
  49. {
  50. subdomain = curSub;
  51. }
  52. else
  53. {
  54. if (routeData.Values["sub"] == null)
  55. {
  56. routeData.Values["sub"] = subdomain;
  57. }
  58. else
  59. {
  60. subdomain = routeData.Values["sub"].ToString();
  61. }
  62. }
  63. //routeData.Values["sub"] = subdomain;
  64. if (Subdomains.Contains("*") || Subdomains.Contains(subdomain))
  65. {
  66. return routeData;
  67. }
  68. return null;
  69. }
  70. public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
  71. {
  72. object subdomainParam = requestContext.HttpContext.Request.QueryString["sub"];
  73. if (subdomainParam != null && values["sub"] == null)
  74. values["sub"] = subdomainParam;
  75. return base.GetVirtualPath(requestContext, values); // we now have the route based on subdomain
  76. }
  77. }
  78. }