예제 #1
0
        public INavigationTarget CreateNavigationTarget(string url)
        {
            if (!url.StartsWith(_scheme))
            {
                url = $"{_scheme}{url}";
            }

            var uriInfo = new UriInfo(url);
            var route   = FindRouteOrDefault(url);

            var parameters = ExtractParameters(route, uriInfo);
            var ctor       = GetConstructor(route.TargetType, parameters);

            if (ctor == null)
            {
                throw new Exception($"Can't find constructor in {route.TargetType} to match {parameters.Count} parameters {String.Join(", ", parameters)}");
            }

            var ctorparams = ctor.GetParameters();

            var parameterValues = new List <object>();

            foreach (var item in parameters)
            {
                var param = ctorparams.FirstOrDefault(x => x.Name.ToLowerInvariant() == item.Key.ToLowerInvariant());
                if (param != null)
                {
                    var filledparam = ChangeType(item.Value, param.ParameterType);
                    parameterValues.Add(filledparam);
                }
            }
            var target = CreateTarget(ctor, parameterValues, url);

            return(target);
        }
예제 #2
0
        private Dictionary <string, string> ExtractParameters(Route route, UriInfo uriInfo)
        {
            var parameters = new Dictionary <string, string>();
            var segments   = route.CreateSegments();

            for (var i = 0; i < segments.Count; i++)
            {
                if (segments[i].IsVariable && uriInfo.Segments.Length > i)
                {
                    parameters.Add(segments[i].Segment.ToLowerInvariant(), uriInfo.Segments[i]);
                }
            }

            foreach (var qs in uriInfo.QueryString)
            {
                parameters.Add(qs.Key.ToLowerInvariant(), qs.Value);
            }

            return(parameters);
        }