The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SubdomainRoute.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Web;
  6. using Microsoft.AspNetCore.Routing;
  7. using Teknik.Utilities;
  8. namespace Teknik.Utilities
  9. {
  10. public class SubdomainRoute : Route
  11. {
  12. private readonly IRouter _target;
  13. public List<string> Subdomains { get; set; }
  14. public List<string> Domains { get; set; }
  15. public SubdomainRoute(List<string> subdomains, List<string> domains, IRouter router, string routeTemplate, IInlineConstraintResolver inlineConstraintResolver)
  16. : base(router, routeTemplate, inlineConstraintResolver)
  17. {
  18. this.Subdomains = subdomains;
  19. this.Domains = domains;
  20. this._target = router;
  21. }
  22. public SubdomainRoute(List<string> subdomains, List<string> domains, IRouter router, string routeTemplate, RouteValueDictionary defaults, IDictionary<string, object> constraints, RouteValueDictionary dataTokens, IInlineConstraintResolver inlineConstraintResolver)
  23. : base(router, routeTemplate, defaults, constraints, dataTokens, inlineConstraintResolver)
  24. {
  25. this.Subdomains = subdomains;
  26. this.Domains = domains;
  27. this._target = router;
  28. }
  29. public SubdomainRoute(List<string> subdomains, List<string> domains, IRouter router, string routeName, string routeTemplate, RouteValueDictionary defaults, IDictionary<string, object> constraints, RouteValueDictionary dataTokens, IInlineConstraintResolver inlineConstraintResolver)
  30. : base(router, routeName, routeTemplate, defaults, constraints, dataTokens, inlineConstraintResolver)
  31. {
  32. this.Subdomains = subdomains;
  33. this.Domains = domains;
  34. this._target = router;
  35. }
  36. protected override Task OnRouteMatched(RouteContext context)
  37. {
  38. // Only look at the subdomain if there is any route data.
  39. if (context.RouteData != null)
  40. {
  41. string subdomain = context.HttpContext.Request.Query["sub"]; // A subdomain specified as a query parameter takes precedence over the hostname.
  42. string host = context.HttpContext.Request.Headers["Host"];
  43. if (host != null)
  44. {
  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 (context.RouteData.Values["sub"] == null)
  63. {
  64. context.RouteData.Values["sub"] = subdomain;
  65. }
  66. else
  67. {
  68. subdomain = context.RouteData.Values["sub"].ToString();
  69. }
  70. }
  71. // Check if this route is valid for the current domain
  72. if (context.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. context.RouteData.Values["sub"] = subdomain;
  78. context.RouteData.Routers.Add(_target);
  79. return _target.RouteAsync(context);
  80. }
  81. }
  82. }
  83. }
  84. return Task.CompletedTask;
  85. }
  86. public override VirtualPathData GetVirtualPath(VirtualPathContext context)
  87. {
  88. object subdomainParam = context.HttpContext.Request.Query["sub"];
  89. if (subdomainParam != null && context.Values["sub"] == null)
  90. context.Values["sub"] = subdomainParam;
  91. return base.GetVirtualPath(context); // we now have the route based on subdomain
  92. }
  93. }
  94. }