Example #1
0
        public async virtual Task RouteAsync([NotNull] RouteContext context)
        {
            EnsureLoggers(context.HttpContext);

            var requestPath = context.HttpContext.Request.Path.Value;

            if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
            {
                requestPath = requestPath.Substring(1);
            }

            var values = _matcher.Match(requestPath);

            if (values == null)
            {
                // 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);

            if (!RouteConstraintMatcher.Match(
                    Constraints,
                    newRouteData.Values,
                    context.HttpContext,
                    this,
                    RouteDirection.IncomingRequest,
                    _constraintLogger))
            {
                return;
            }

            _logger.LogVerbose(
                "Request successfully matched the route with name '{RouteName}' and template '{RouteTemplate}'.",
                Name,
                RouteTemplate);

            try
            {
                context.RouteData = newRouteData;

                await _target.RouteAsync(context);
            }
            finally
            {
                // Restore the original values to prevent polluting the route data.
                if (!context.IsHandled)
                {
                    context.RouteData = oldRouteData;
                }
            }
        }
Example #2
0
        public async virtual Task RouteAsync(RouteContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            EnsureLoggers(context.HttpContext);

            var requestPath = context.HttpContext.Request.Path;
            var values      = _matcher.Match(requestPath);

            if (values == null)
            {
                // 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);

            // Perf: Avoid accessing data tokens if you don't need to write to it, these dictionaries are all
            // created lazily.
            if (_dataTokens.Count > 0)
            {
                MergeValues(newRouteData.DataTokens, _dataTokens);
            }

            newRouteData.Routers.Add(_target);
            MergeValues(newRouteData.Values, values);

            if (!RouteConstraintMatcher.Match(
                    Constraints,
                    newRouteData.Values,
                    context.HttpContext,
                    this,
                    RouteDirection.IncomingRequest,
                    _constraintLogger))
            {
                return;
            }

            _logger.LogVerbose(
                "Request successfully matched the route with name '{RouteName}' and template '{RouteTemplate}'.",
                Name,
                RouteTemplate);

            try
            {
                context.RouteData = newRouteData;

                await _target.RouteAsync(context);
            }
            finally
            {
                // Restore the original values to prevent polluting the route data.
                if (!context.IsHandled)
                {
                    context.RouteData = oldRouteData;
                }
            }
        }
Example #3
0
        public async virtual Task RouteAsync([NotNull] RouteContext context)
        {
            EnsureLoggers(context.HttpContext);
            using (_logger.BeginScope("TemplateRoute.RouteAsync"))
            {
                var requestPath = context.HttpContext.Request.Path.Value;

                if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
                {
                    requestPath = requestPath.Substring(1);
                }

                var values = _matcher.Match(requestPath, Defaults);

                if (values == null)
                {
                    if (_logger.IsEnabled(TraceType.Verbose))
                    {
                        _logger.WriteValues(CreateRouteAsyncValues(
                                                requestPath,
                                                values,
                                                matchedValues: false,
                                                matchedConstraints: false,
                                                handled: context.IsHandled));
                    }

                    // If we got back a null value set, that means the URI did not match
                    return;
                }
                else
                {
                    // Not currently doing anything to clean this up if it's not a match. Consider hardening this.
                    context.RouteData.Values = values;

                    if (RouteConstraintMatcher.Match(Constraints,
                                                     values,
                                                     context.HttpContext,
                                                     this,
                                                     RouteDirection.IncomingRequest,
                                                     _constraintLogger))
                    {
                        context.RouteData.DataTokens = _dataTokens;
                        await _target.RouteAsync(context);

                        if (_logger.IsEnabled(TraceType.Verbose))
                        {
                            _logger.WriteValues(CreateRouteAsyncValues(
                                                    requestPath,
                                                    values,
                                                    matchedValues: true,
                                                    matchedConstraints: true,
                                                    handled: context.IsHandled));
                        }
                    }
                    else
                    {
                        if (_logger.IsEnabled(TraceType.Verbose))
                        {
                            _logger.WriteValues(CreateRouteAsyncValues(
                                                    requestPath,
                                                    values,
                                                    matchedValues: true,
                                                    matchedConstraints: false,
                                                    handled: context.IsHandled));
                        }
                    }
                }
            }
        }
        public async virtual Task RouteAsync([NotNull] RouteContext context)
        {
            EnsureLoggers(context.HttpContext);
            using (_logger.BeginScope("TemplateRoute.RouteAsync"))
            {
                var requestPath = context.HttpContext.Request.Path.Value;

                if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
                {
                    requestPath = requestPath.Substring(1);
                }

                var values = _matcher.Match(requestPath);

                if (values == null)
                {
                    if (_logger.IsEnabled(LogLevel.Verbose))
                    {
                        _logger.WriteValues(CreateRouteAsyncValues(
                                                requestPath,
                                                context.RouteData.Values,
                                                matchedValues: false,
                                                matchedConstraints: false,
                                                handled: context.IsHandled));
                    }

                    // 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);

                if (!RouteConstraintMatcher.Match(
                        Constraints,
                        newRouteData.Values,
                        context.HttpContext,
                        this,
                        RouteDirection.IncomingRequest,
                        _constraintLogger))
                {
                    if (_logger.IsEnabled(LogLevel.Verbose))
                    {
                        _logger.WriteValues(CreateRouteAsyncValues(
                                                requestPath,
                                                newRouteData.Values,
                                                matchedValues: true,
                                                matchedConstraints: false,
                                                handled: context.IsHandled));
                    }

                    return;
                }

                try
                {
                    context.RouteData = newRouteData;

                    await _target.RouteAsync(context);

                    if (_logger.IsEnabled(LogLevel.Verbose))
                    {
                        _logger.WriteValues(CreateRouteAsyncValues(
                                                requestPath,
                                                newRouteData.Values,
                                                matchedValues: true,
                                                matchedConstraints: true,
                                                handled: context.IsHandled));
                    }
                }
                finally
                {
                    // Restore the original values to prevent polluting the route data.
                    if (!context.IsHandled)
                    {
                        context.RouteData = oldRouteData;
                    }
                }
            }
        }