asp.net-mvc-5 – MVC5中的域路由

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-5 – MVC5中的域路由前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我知道有一些关于类似事情的文章,但没有一篇对我有帮助.

我有一个单独的Web应用程序,其中多个域指向同一个应用程序.我想知道如何根据尝试访问Web应用程序的域将路由映射到特定控制器.

我目前尝试的是以下但没有成功.

  1. routes.MapRoute(
  2. name: "WowPoetry",url: "wowpoetry.org/{action}/{id}",defaults: new { controller = "Wow",action = "Index",id = UrlParameter.Optional }
  3. );

解决方法

我最终遵循本教程来实现我想要的,除了我以前做域路由而不是子域路由.

Domain Routing Tutorial ASP.Net MVC

执行:

  1. routes.Add(new DomainRoute("wowpoetry.org","",new { controller = "Wow",action = "Index" }));

DomainData.cs

  1. public class DomainData
  2. {
  3. public string Protocol { get; set; }
  4. public string HostName { get; set; }
  5. public string Fragment { get; set; }
  6. }

DomainRoute.cs

  1. public class DomainRoute : Route
  2. {
  3. private Regex domainRegex;
  4. private Regex pathRegex;
  5.  
  6. public string Domain { get; set; }
  7.  
  8. public DomainRoute(string domain,string url,RouteValueDictionary defaults)
  9. : base(url,defaults,new MvcRouteHandler())
  10. {
  11. Domain = domain;
  12. }
  13.  
  14. public DomainRoute(string domain,RouteValueDictionary defaults,IRouteHandler routeHandler)
  15. : base(url,routeHandler)
  16. {
  17. Domain = domain;
  18. }
  19.  
  20. public DomainRoute(string domain,object defaults)
  21. : base(url,new RouteValueDictionary(defaults),object defaults,routeHandler)
  22. {
  23. Domain = domain;
  24. }
  25.  
  26. public override RouteData GetRouteData(HttpContextBase httpContext)
  27. {
  28. // Build regex
  29. domainRegex = CreateRegex(Domain);
  30. pathRegex = CreateRegex(Url);
  31.  
  32. // Request information
  33. string requestDomain = httpContext.Request.Headers["host"];
  34. if (!string.IsNullOrEmpty(requestDomain))
  35. {
  36. if (requestDomain.IndexOf(":") > 0)
  37. {
  38. requestDomain = requestDomain.Substring(0,requestDomain.IndexOf(":"));
  39. }
  40. }
  41. else
  42. {
  43. requestDomain = httpContext.Request.Url.Host;
  44. }
  45. string requestPath = httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(2) +
  46. httpContext.Request.PathInfo;
  47.  
  48. // Match domain and route
  49. Match domainMatch = domainRegex.Match(requestDomain);
  50. Match pathMatch = pathRegex.Match(requestPath);
  51.  
  52. // Route data
  53. RouteData data = null;
  54. if (domainMatch.Success && pathMatch.Success && requestDomain.ToLower() != "tg.local" &&
  55. requestDomain.ToLower() != "tg.terrasynq.net" && requestDomain.ToLower() != "www.townsgossip.com" &&
  56. requestDomain.ToLower() != "townsgossip.com")
  57. {
  58. data = new RouteData(this,RouteHandler);
  59.  
  60. // Add defaults first
  61. if (Defaults != null)
  62. {
  63. foreach (KeyValuePair<string,object> item in Defaults)
  64. {
  65. data.Values[item.Key] = item.Value;
  66. }
  67. }
  68.  
  69. // Iterate matching domain groups
  70. for (int i = 1; i < domainMatch.Groups.Count; i++)
  71. {
  72. Group group = domainMatch.Groups[i];
  73. if (group.Success)
  74. {
  75. string key = domainRegex.GroupNameFromNumber(i);
  76.  
  77. if (!string.IsNullOrEmpty(key) && !char.IsNumber(key,0))
  78. {
  79. if (!string.IsNullOrEmpty(group.Value))
  80. {
  81. data.Values[key] = group.Value;
  82. }
  83. }
  84. }
  85. }
  86.  
  87. // Iterate matching path groups
  88. for (int i = 1; i < pathMatch.Groups.Count; i++)
  89. {
  90. Group group = pathMatch.Groups[i];
  91. if (group.Success)
  92. {
  93. string key = pathRegex.GroupNameFromNumber(i);
  94.  
  95. if (!string.IsNullOrEmpty(key) && !char.IsNumber(key,0))
  96. {
  97. if (!string.IsNullOrEmpty(group.Value))
  98. {
  99. data.Values[key] = group.Value;
  100. }
  101. }
  102. }
  103. }
  104. }
  105.  
  106. return data;
  107. }
  108.  
  109. public override VirtualPathData GetVirtualPath(RequestContext requestContext,RouteValueDictionary values)
  110. {
  111. return base.GetVirtualPath(requestContext,RemoveDomainTokens(values));
  112. }
  113.  
  114. public DomainData GetDomainData(RequestContext requestContext,RouteValueDictionary values)
  115. {
  116. // Build hostname
  117. string hostname = Domain;
  118. foreach (KeyValuePair<string,object> pair in values)
  119. {
  120. hostname = hostname.Replace("{" + pair.Key + "}",pair.Value.ToString());
  121. }
  122.  
  123. // Return domain data
  124. return new DomainData
  125. {
  126. Protocol = "http",HostName = hostname,Fragment = ""
  127. };
  128. }
  129.  
  130. private Regex CreateRegex(string source)
  131. {
  132. // Perform replacements
  133. source = source.Replace("/",@"\/?");
  134. source = source.Replace(".",@"\.?");
  135. source = source.Replace("-",@"\-?");
  136. source = source.Replace("{",@"(?<");
  137. source = source.Replace("}",@">([a-zA-Z0-9_\-]*))");
  138.  
  139. return new Regex("^" + source + "$");
  140. }
  141.  
  142. private RouteValueDictionary RemoveDomainTokens(RouteValueDictionary values)
  143. {
  144. var tokenRegex =
  145. new Regex(
  146. @"({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?({[a-zA-Z0-9_\-]*})*\.?\/?");
  147. Match tokenMatch = tokenRegex.Match(Domain);
  148. for (int i = 0; i < tokenMatch.Groups.Count; i++)
  149. {
  150. Group group = tokenMatch.Groups[i];
  151. if (group.Success)
  152. {
  153. string key = group.Value.Replace("{","").Replace("}","");
  154. if (values.ContainsKey(key))
  155. values.Remove(key);
  156. }
  157. }
  158.  
  159. return values;
  160. }
  161. }

猜你在找的asp.Net相关文章