ASP.NET Web API RTM and subdomain routes

后端 未结 2 1537
终归单人心
终归单人心 2021-02-10 14:42

I am having issues getting Web API to work with subdomain-based routes. In short, I am getting to the right controller and method but the data token out of the subdomain is not

相关标签:
2条回答
  • 2021-02-10 15:20

    Thanks for reporting the issue. I used your repro at https://github.com/woloski/AspNetWebApiWithSubdomains and did some debugging.

    Here is why it is happening. The HttpDomainRoute.GetRouteData is not being called because it was wrapped by an internal class called HttpWebRoute in Web API. When you use config.Routes.Add method to add your custom route, instead of calling the HttpDomainRoute.GetRouteData, it will simply call the System.Web.Routing.Route's implementation of GetRouteData. That is why you see the rest of parameters being mapped correctly except the tenant.

    I can't think of any easy workaround on top of my head. I can file a issue at the codeplex site at http://aspnetwebstack.codeplex.com/ to track this issue.

    0 讨论(0)
  • 2021-02-10 15:21

    After thinking of this more, I have a workaround for you. The basic idea of the workaround is to use a route that derives from Route and add it to the RouteCollection directly. That way, the route we are passing will no longer be wrapped inside HttpWebRoute.

    The RouteByPassing handler is to workaround another known issue. Hope this helps.

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
    
            RouteTable.Routes.Add("test", new HttpDomainRoute(
                domain: "{tenant}.auth10.com",
                routeTemplate: "test",
                defaults: new { controller = "Values", action = "GetTenant" }
            ));
    
            config.MessageHandlers.Add(new RouteByPassingHandler());
        }
    }
    
    public class RouteByPassingHandler : DelegatingHandler
    {
        protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
        {
            HttpMessageInvoker invoker = new HttpMessageInvoker(new HttpControllerDispatcher(request.GetConfiguration()));
            return invoker.SendAsync(request, cancellationToken);
        }
    }
    
    public class HttpDomainRoute
        : Route
    {
        private Regex domainRegex;
        private Regex pathRegex;
    
        public HttpDomainRoute(string domain, string routeTemplate, object defaults, object constraints = null)
            : base(routeTemplate, new RouteValueDictionary(defaults), new RouteValueDictionary(constraints), new RouteValueDictionary(), HttpControllerRouteHandler.Instance)
        {
            this.Domain = domain;
        }
    
        public string Domain { get; set; }
    
        public override RouteData GetRouteData(HttpContextBase context) 
        {   
            // Build regex
            domainRegex = CreateRegex(this.Domain);
            pathRegex = CreateRegex(this.Url);
    
            // Request information
            string requestDomain = context.Request.Headers["Host"];
            if (!string.IsNullOrEmpty(requestDomain))
            {
                if (requestDomain.IndexOf(":") > 0)
                {
                    requestDomain = requestDomain.Substring(0, requestDomain.IndexOf(":"));
                }
            }
            else
            {
                requestDomain = context.Request.Url.Host;
            }
    
            string requestPath = context.Request.AppRelativeCurrentExecutionFilePath.Substring(2) + context.Request.PathInfo;
    
            // Match domain and route
            Match domainMatch = domainRegex.Match(requestDomain);
            Match pathMatch = pathRegex.Match(requestPath);
    
            // Route data
            RouteData data = null;
            if (domainMatch.Success && pathMatch.Success)
            {
                data = base.GetRouteData(context);
    
                // Add defaults first
                if (Defaults != null)
                {
                    foreach (KeyValuePair<string, object> item in Defaults)
                    {
                        data.Values[item.Key] = item.Value;
                    }
                }
    
                // Iterate matching domain groups
                for (int i = 1; i < domainMatch.Groups.Count; i++)
                {
                    Group group = domainMatch.Groups[i];
                    if (group.Success)
                    {
                        string key = domainRegex.GroupNameFromNumber(i);
    
                        if (!string.IsNullOrEmpty(key) && !char.IsNumber(key, 0))
                        {
                            if (!string.IsNullOrEmpty(group.Value))
                            {
                                data.Values[key] = group.Value;
                            }
                        }
                    }
                }
    
                // Iterate matching path groups
                for (int i = 1; i < pathMatch.Groups.Count; i++)
                {
                    Group group = pathMatch.Groups[i];
                    if (group.Success)
                    {
                        string key = pathRegex.GroupNameFromNumber(i);
    
                        if (!string.IsNullOrEmpty(key) && !char.IsNumber(key, 0))
                        {
                            if (!string.IsNullOrEmpty(group.Value))
                            {
                                data.Values[key] = group.Value;
                            }
                        }
                    }
                }
            }
    
            return data;
        }
    
        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) 
        {
            return base.GetVirtualPath(requestContext, RemoveDomainTokens(values));
        }
    
        private Regex CreateRegex(string source)
        {
            // Perform replacements
            source = source.Replace("/", @"\/?");
            source = source.Replace(".", @"\.?");
            source = source.Replace("-", @"\-?");
            source = source.Replace("{", @"(?<");
            source = source.Replace("}", @">([a-zA-Z0-9_-]*))");
    
            return new Regex("^" + source + "$");
        }
    
        private RouteValueDictionary RemoveDomainTokens(RouteValueDictionary values)
        {
            Regex tokenRegex = new Regex(@"({[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_-]*})*-?\.?\/?");
            Match tokenMatch = tokenRegex.Match(Domain);
            for (int i = 0; i < tokenMatch.Groups.Count; i++)
            {
                Group group = tokenMatch.Groups[i];
                if (group.Success)
                {
                    string key = group.Value.Replace("{", "").Replace("}", "");
                    if (values.ContainsKey(key))
                        values.Remove(key);
                }
            }
    
            return values;
        }
    }
    

    }

    0 讨论(0)
提交回复
热议问题