Esempio n. 1
0
        private bool TryResolvePath(ContentHint input, [MaybeNullWhen(returnValue: false)] out WebPath path)
        {
            var resolved = new PathBuilder(Routing.Path.TrailingSlash);

            foreach (var segment in Routing.Path.Parts)
            {
                if (segment.StartsWith(':'))
                {
                    if (input.TryGetValue(segment.Substring(1), out var value))
                    {
                        if (value != null)
                        {
                            resolved.Append(Convert.ToString(value, CultureInfo.InvariantCulture));
                        }
                        else
                        {
                            path = default;
                            return(false);
                        }
                    }
                    else
                    {
                        path = default;
                        return(false);
                    }
                }
                else
                {
                    resolved.Append(segment);
                }
            }

            path = resolved.Build();
            return(true);
        }
Esempio n. 2
0
        private bool TrySimulateArguments(IRequest request, ContentHint input, [MaybeNullWhen(returnValue: false)] out object?[] arguments)
        {
            var targetParameters = Method.GetParameters();

            var targetArguments = new object?[targetParameters.Length];

            if (TryResolvePath(input, out var path))
            {
                request.Properties[$"_{ID}_matchedPath"] = path;
            }
            else
            {
                arguments = default;
                return(false);
            }

            for (int i = 0; i < targetParameters.Length; i++)
            {
                var par = targetParameters[i];

                // request
                if (par.ParameterType == typeof(IRequest))
                {
                    targetArguments[i] = request;
                    continue;
                }

                // handler
                if (par.ParameterType == typeof(IHandler))
                {
                    targetArguments[i] = this;
                    continue;
                }

                // set from the given input set
                if (input.TryGetValue(par.Name, out var value))
                {
                    targetArguments[i] = value;
                    continue;
                }
            }

            arguments = targetArguments;
            return(true);
        }
Esempio n. 3
0
        private bool TryGetHandler(IRequest request, ContentHint input, [MaybeNullWhen(returnValue: false)] out IHandler handler)
        {
            if (Method.ReturnType == typeof(IHandlerBuilder) || Method.ReturnType == typeof(IHandler))
            {
                if (TrySimulateArguments(request, input, out var arguments))
                {
                    var result = Invoke(arguments);

                    if (result is IHandlerBuilder builder)
                    {
                        handler = builder.Build(this);
                        return(true);
                    }
                    else if (result is IHandler resultHandler)
                    {
                        handler = resultHandler;
                        return(true);
                    }
                }
            }

            handler = default;
            return(false);
        }