/// <summary> /// Builds an <see cref="Route"/> for a particular action. /// </summary> /// <param name="routeTemplate">The tokenized route template for the route.</param> /// <param name="routeInfoProvider">The info provider for the route.</param> /// <param name="controllerDescriptor">The controller the route attribute has been applied on.</param> /// <param name="actionDescriptor">The action reachable by this route.</param> /// <returns>The generated <see cref="Route"/>.</returns> public Route BuildDirectRoute( string routeTemplate, IRouteInfoProvider routeInfoProvider, ControllerDescriptor controllerDescriptor, ActionDescriptor actionDescriptor) { return(BuildDirectRoute(routeTemplate, routeInfoProvider, controllerDescriptor, new ActionDescriptor[] { actionDescriptor }, isActionDirectRoute: true)); }
/// <summary> /// Builds an <see cref="Route"/> for a particular controller. /// </summary> /// <param name="routeTemplate">The tokenized route template for the route.</param> /// <param name="routeInfoProvider">The info provider for the route.</param> /// <param name="controllerDescriptor">The controller the route attribute has been applied on.</param> /// <param name="actionDescriptors">The actions reachable by this route.</param> /// <returns>The generated <see cref="Route"/>.</returns> public Route BuildDirectRoute( string routeTemplate, IRouteInfoProvider routeInfoProvider, ControllerDescriptor controllerDescriptor, IEnumerable <ActionDescriptor> actionDescriptors) { return(BuildDirectRoute(routeTemplate, routeInfoProvider, controllerDescriptor, actionDescriptors, isActionDirectRoute: false)); }
private RouteEntry CreateRouteEntry(string areaPrefix, string prefix, IRouteInfoProvider provider, ControllerDescriptor controller, IList <ActionDescriptor> actions, bool routeIsForAction) { ValidateTemplate(provider.Template, actions[0].ActionName, controller); string template = CombinePrefixAndAreaWithTemplate(areaPrefix, prefix, provider.Template); Route route = _routeBuilder.BuildDirectRoute(template, provider, controller, actions, routeIsForAction); RouteEntry entry = new RouteEntry { Name = provider.Name, Route = route, Template = template, }; return(entry); }
/// <summary> /// Builds an <see cref="Route"/> for a particular controller. /// </summary> /// <param name="routeTemplate">The tokenized route template for the route.</param> /// <param name="routeInfoProvider">The info provider for the route.</param> /// <param name="controllerDescriptor">The controller the route attribute has been applied on.</param> /// <param name="actionDescriptors">The actions reachable by this route.</param> /// <param name="routeIsForAction">Whether or not the direct route is for an action.</param> /// <returns>The generated <see cref="Route"/>.</returns> public Route BuildDirectRoute( string routeTemplate, IRouteInfoProvider routeInfoProvider, ControllerDescriptor controllerDescriptor, IEnumerable <ActionDescriptor> actionDescriptors, bool routeIsForAction) { if (routeTemplate == null) { throw Error.ArgumentNull("routeTemplate"); } if (routeInfoProvider == null) { throw Error.ArgumentNull("routeInfoProvider"); } if (controllerDescriptor == null) { throw Error.ArgumentNull("controllerDescriptor"); } if (actionDescriptors == null || !actionDescriptors.Any()) { throw Error.ParameterCannotBeNullOrEmpty("actionDescriptors"); } string controllerName = controllerDescriptor.ControllerName; RouteAreaAttribute area = controllerDescriptor.GetAreaFrom(); string areaName = controllerDescriptor.GetAreaName(area); RouteValueDictionary defaults = new RouteValueDictionary { { "controller", controllerName } }; if (routeIsForAction) { ActionDescriptor actionDescriptor = actionDescriptors.Single(); defaults.Add("action", actionDescriptor.ActionName); } RouteValueDictionary constraints = new RouteValueDictionary(); string detokenizedRouteTemplate = InlineRouteTemplateParser.ParseRouteTemplate(routeTemplate, defaults, constraints, ConstraintResolver); ParsedRoute parsedRoute = RouteParser.Parse(detokenizedRouteTemplate); RouteValueDictionary dataTokens = new RouteValueDictionary(); dataTokens[RouteDataTokenKeys.DirectRoutePrecedence] = RoutePrecedence.Compute(parsedRoute, constraints); dataTokens[RouteDataTokenKeys.DirectRouteController] = controllerDescriptor; dataTokens[RouteDataTokenKeys.DirectRouteActions] = actionDescriptors; int order = 0; IOrderedRouteInfoProvider orderedAttribute = routeInfoProvider as IOrderedRouteInfoProvider; if (orderedAttribute != null) { order = orderedAttribute.Order; } dataTokens[RouteDataTokenKeys.DirectRouteOrder] = order; if (areaName != null) { dataTokens.Add(RouteDataTokenKeys.Area, areaName); dataTokens.Add(RouteDataTokenKeys.UseNamespaceFallback, value: false); Type controllerType = controllerDescriptor.ControllerType; if (controllerType != null) { dataTokens.Add(RouteDataTokenKeys.Namespaces, new[] { controllerType.Namespace }); } } Route route = new Route(detokenizedRouteTemplate, new MvcRouteHandler()) { Defaults = defaults, Constraints = constraints, DataTokens = dataTokens }; return(route); }