/// <summary> /// Parses the specified route template string, placing the available information in /// <see cref="info" />. /// </summary> /// <param name="template">Template to parse</param> /// <param name="info">RouteInfo to store parsed info</param> public void Parse(RouteTemplate template, RouteInfo info) { var builder = new StringBuilder(); foreach (var segment in template.Segments) { foreach (var part in segment.Parts) { if (part.IsLiteral) { builder.Append(part.Text); continue; } if (part.DefaultValue != null) { info.Defaults[part.Name] = part.DefaultValue; } if (part.IsOptional) { info.Optional.Add(part.Name); } builder.Append('{'); builder.Append(part.Name); builder.Append('}'); // TODO part.IsOptionalSeperator // TODO part.IsCatchAll } builder.Append('/'); } info.Url = builder.ToString().TrimEnd('/'); }
/// <summary> /// Process the constraints of the specified route /// </summary> /// <param name="constraints">Constraints to process</param> /// <param name="routeInfo">Output information about the route</param> public void ProcessConstraints(RouteValueDictionary constraints, RouteInfo routeInfo) { Contract.Requires(routeInfo.Constraints != null); // Only add strings to the route info var supportedConstraints = constraints.Where(constraint => constraint.Value is string); foreach (var kvp in supportedConstraints) { routeInfo.Constraints.Add(kvp.Key, kvp.Value); } }
/// <summary> /// Gets useful information from the specified template route /// </summary> /// <param name="route">Template route to get information from</param> /// <returns>Information from the route</returns> private RouteInfo ProcessTemplateRoute(TemplateRoute route) { var info = new RouteInfo { Constraints = _constraintsProcessor.ProcessConstraints(route.Constraints), Defaults = route.Defaults.ToDictionary(x => x.Key.ToLowerInvariant(), x => x.Value), Optional = new List<string>(), }; var template = TemplateParser.Parse(route.RouteTemplate); _parser.Parse(template, info); return info; }
/// <summary> /// Gets information about the specified route /// </summary> /// <param name="route">The route</param> /// <returns>Route information</returns> private RouteInfo GetRoute(Route route) { var routeInfo = new RouteInfo { Url = route.Url, Constraints = route.Constraints ?? new RouteValueDictionary() }; foreach (var processor in _defaultsProcessors) { processor.ProcessDefaults(route, routeInfo); } return routeInfo; }
/// <summary> /// Gets information about the specified route /// </summary> /// <param name="route">The route</param> /// <returns>Route information</returns> private RouteInfo GetRoute(Route route) { var routeInfo = new RouteInfo { Url = route.Url, Constraints = new RouteValueDictionary(), }; foreach (var processor in _defaultsProcessors) { processor.ProcessDefaults(route, routeInfo); } if (route.Constraints != null) { foreach (var processor in _constraintsProcessors) { processor.ProcessConstraints(route.Constraints, routeInfo); } } return(routeInfo); }
/// <summary> /// Check whether the specified route should be exposed in the JavaScript output /// </summary> /// <param name="routeInfo">Route to check</param> /// <returns><c>false</c> if the route should definitely be blocked, <c>true</c> if the route should be exposed (or unsure)</returns> public bool AllowRoute(RouteInfo routeInfo) { // No controller name, no idea what to do with it. // Let another filter take care of this if (!routeInfo.Defaults.ContainsKey("controller")) { return true; } var controller = (string)routeInfo.Defaults["controller"]; // If explicitly blacklisted, always deny if (_controllerBlacklist.Contains(controller)) { return false; } // If explicitly whitelisted, always allow if (_controllerWhitelist.Contains(controller)) { return true; } // Otherwise, allow based on configuration return _configuration.ExposeAllRoutes; }