Exemple #1
0
    public TemplateMatcher(
        RouteTemplate template,
        RouteValueDictionary defaults)
    {
        if (template == null)
        {
            throw new ArgumentNullException(nameof(template));
        }

        Template = template;
        Defaults = defaults ?? new RouteValueDictionary();

        // Perf: cache the default value for each parameter (other than complex segments).
        _hasDefaultValue = new bool[Template.Segments.Count];
        _defaultValues   = new object[Template.Segments.Count];

        for (var i = 0; i < Template.Segments.Count; i++)
        {
            var segment = Template.Segments[i];
            if (!segment.IsSimple)
            {
                continue;
            }

            var part = segment.Parts[0];
            if (!part.IsParameter)
            {
                continue;
            }

            if (Defaults.TryGetValue(part.Name !, out var value))
            {
                _hasDefaultValue[i] = true;
                _defaultValues[i]   = value;
            }
        }

        var routePattern = Template.ToRoutePattern();

        _routePatternMatcher = new RoutePatternMatcher(routePattern, Defaults);
    }
Exemple #2
0
        public async Task Invoke(HttpContext ctx)
        {
            ctx.Response.StatusCode = 404;
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            RouteValueDictionary values = new RouteValueDictionary();

            if (matcher.TryMatch(ctx.Request.Path, values))
            {
                _logger.LogInformation("Deu match: " + values["id"]);
                _logger.LogInformation($"Tempo para match: {stopwatch.ElapsedMilliseconds}ms");
                stopwatch.Restart();

                var endpointFeature = ctx.Features[typeof(IEndpointFeature)] as IEndpointFeature;
                endpointFeature.Endpoint = new RouteEndpoint(_next, template.ToRoutePattern(), 1, null, endpointFeature.Endpoint.DisplayName);

                ctx.Request.RouteValues = values;

                _logger.LogInformation($"Tempo para configuração da rota: {stopwatch.ElapsedMilliseconds}ms");
                stopwatch.Restart();
                var devMid = _assemblyLoader.LoadMiddleware("DevMiddleware.DevMiddleware", values["id"].ToString());
                _logger.LogInformation($"Tempo de carga do Middleware: {stopwatch.ElapsedMilliseconds}ms");
                stopwatch.Restart();
                // devMidType.GetMethod("Invoke").Invoke(devMid, new object[] {ctx, null});
                await devMid.Invoke(ctx, null);

                _logger.LogInformation($"Tempo de execução do middleware: {stopwatch.ElapsedMilliseconds}ms");
                stopwatch.Restart();
            }
            else
            {
                await ctx.Response.WriteAsync("Not found");
            }
            stopwatch.Stop();
        }