Example #1
0
        public NavigationTarget CreateTarget(string url)
        {
            if (!url.StartsWith(Scheme))
            {
                url = string.Format("{0}{1}", Scheme, url);
            }

            var uriInfo = new UriInfo(url);
            var route = FindRoute(uriInfo.Path);

            if (route == null)
            {
                throw new Exception("Could not find a route for '" + url + "'.");
            }

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

            if (cons == null)
            {
                throw new Exception("Could not find a constructor in type " + route.TargetType.Name + " that matches '" + url + "'.");
            }

            var ctorparams = cons.GetParameters();
            var parameterValues = ctorparams.Select(p =>
            {
                var value = parameters[p.Name.ToLowerInvariant()];
                return ChangeType(value, p.ParameterType);
            });

            var target = cons.Invoke(parameterValues.ToArray()) as NavigationTarget;
            return target;
        }
 public void When_url_contains_a_scheme_then_should_not_throw()
 {
     const string url = "test://S/95368018";
     var info = new UriInfo(url);
     Assert.AreEqual(2, info.Segments.Count);
 }
Example #3
0
 private Dictionary<string, string> ExtractParameters(Route route, UriInfo uriInfo)
 {
     var parameters = new Dictionary<string, string>();
     for (int i = 0; i < route.Segments.Count; i++)
     {
         if (route.Segments[i].IsVariable)
         {
             parameters.Add(route.Segments[i].Name.ToLowerInvariant(), uriInfo.Segments[i]);
         }
     }
     foreach (var qs in uriInfo.QueryString)
     {
         parameters.Add(qs.Key.ToLowerInvariant(), qs.Value);
     }
     return parameters;
 }
 public void When_url_does_not_contain_a_scheme_then_should_throw()
 {
     const string url = "S/95368018";
     Assert.ThrowsException<ArgumentException>(() => { var info = new UriInfo(url); });
 }