public static ApiRouter AddWithPath(this ApiRouter router, string path, Action <ApiRouter> configure) { if (path.StartsWith("/")) { path = path.Remove(0, 1); } var segments = path.Split('/'); var currentRouter = router; foreach (var segment in segments) { if (currentRouter.ChildRouters.ContainsKey(segment)) { currentRouter = currentRouter.ChildRouters[segment]; } else { currentRouter.Add(segment, (r) => { currentRouter = r; }); } } configure(currentRouter); return(router); }
public static ApiRouter Search(Type rootType, Uri baseUri) { var typename = GetSegmentFromControllerType(rootType); var router = new ApiRouter("", baseUri).To(rootType); // Find controllers in the namespaces below type var controllers = rootType.Assembly.GetTypes() .Where(t => typeof(IHttpController).IsAssignableFrom(t) && t.Namespace.StartsWith(rootType.Namespace) && t != rootType) .Select(t => new ControllerEntry() { ControllerType = t, Path = t.Namespace.Replace(rootType.Namespace, "").Replace('.', '/') + "/" + GetSegmentFromControllerType(t) }) .ToList(); foreach (var controllerEntry in controllers) { Type localController = controllerEntry.ControllerType; router.AddWithPath(controllerEntry.Path, (r) => { var parameterAttributes = (PathParameterAttribute[])Attribute.GetCustomAttributes(controllerEntry.ControllerType, typeof(PathParameterAttribute)); foreach (var pathParameterAttribute in parameterAttributes) { r.Add(pathParameterAttribute.PathTemplate, (cr) => cr.To(localController)); } // If no path parameters or any are optional then connect the controller to the router if (parameterAttributes.Count() == 0 || parameterAttributes.Any(pa => pa.Required == false)) { r.To(localController); } }); } return(router); }
public ApiRouter Add(ApiRouter childRouter) { _childRouters.Add(childRouter.SegmentTemplate, childRouter); childRouter.ParentRouter = this; return(this); }
public ApiRouter Add(string childSegmentTemplate, Action <ApiRouter> configure) { var childrouter = new ApiRouter(childSegmentTemplate); configure(childrouter); return(Add(childrouter)); }
public Uri GetUrlFromRouter(ApiRouter router) { string url = router.SegmentTemplate; while (router.ParentRouter != null) { router = router.ParentRouter; url = router.SegmentTemplate + @"/" + url; } return(new Uri(_baseUrl, url)); }
public static ApiRouter GetAtPath(this ApiRouter router, string path) { var segments = path.Split('/'); var currentRouter = router; foreach (var segment in segments) { if (currentRouter.ChildRouters.ContainsKey(segment)) { currentRouter = currentRouter.ChildRouters[segment]; } else { return(null); } } return(currentRouter); }
public void AddRouter(ApiRouter router) { _SegmentRoutes.Add(router); }