Ejemplo n.º 1
0
        public async Task RouteAsync(RouteContext context)
        {
            EnsureLoggers(context.HttpContext);
            using (_logger.BeginScope("DomainTemplateRoute.RouteAsync"))
            {
                var requestHost = context.HttpContext.Request.Host.Value;
                if (IgnorePort && requestHost.Contains(":"))
                {
                    requestHost = requestHost.Substring(0, requestHost.IndexOf(":"));
                }
                Console.WriteLine($"Subdomain name: {requestHost}");

                // var routeValues = new RouteValueDictionary();
                // var routeData = context.HttpContext.GetRouteData();
                // var values = _matcher.Match(requestHost);
                var values = _matcher.TryMatch(context.HttpContext.Request.Path, new RouteValueDictionary(context.RouteData));
                //if (values == null)
                if (!values)
                {
                    if (_logger.IsEnabled(LogLevel.Trace))
                    {
                        _logger.LogTrace("DomainTemplateRoute " + Name + " - Host \"" + context.HttpContext.Request.Host + "\" did not match.");
                    }

                    // If we got back a null value set, that means the URI did not match
                    return;
                }

                var oldRouteData = context.RouteData;

                var newRouteData = new RouteData(oldRouteData);
                MergeValues(newRouteData.DataTokens, DataTokens);
                newRouteData.Routers.Add(_target);

                //MergeValues(newRouteData.Values, values.ToImmutableDictionary());
                MergeValues(newRouteData.Values, new RouteValueDictionary(context.RouteData).ToImmutableDictionary());

                try
                {
                    context.RouteData = newRouteData;

                    // delegate further processing to inner route
                    await _innerRoute.RouteAsync(context);
                }
                finally
                {
                    // Restore the original values to prevent polluting the route data.
                    // if (!context.IsHandled)
                    // {
                    //     context.RouteData = oldRouteData;
                    // }
                }
            }
        }