private void MapRoute(RouteCollection routeCollection, ActionInfo action, bool withFormat)
 {
     routeCollection.MapRoute(
         _routing.GetRouteName(action),
         _routing.GetRouteUrl(action) + (withFormat ? ".{format}" : String.Empty),
         new { controller = _routing.GetRouteController(action), action = _routing.GetRouteAction(action), format = UrlParameter.Optional },
         new { method = new RoutingHttpVerbConstraint(_routing.GetRouteConstraint(action))});
 }
        public string GetRouteUrl(ActionInfo action)
        {
            var mapping = GetMappingByNameAndParameters(action);

            switch (mapping.UrlType)
            {
                case UrlType.PluralController:
                    return StringHelpers.Pluralize(action.Controller);
                case UrlType.CustomUrl:
                case UrlType.RootController:
                    if (action.Parameters != null)
                    {
                        return String.Format("{0}{1}{2}", action.Controller, mapping.Url, action.Parameters.Where(p => !p.IsComplexType).Aggregate(String.Empty,
                                                                                                     (current, actionParam) =>
                                                                                                     current + "/{" +
                                                                                                     actionParam.Name + "}"));
                    }
                    return String.Format("{0}{1}", action.Controller, mapping.Url);

                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
 public MappingException(ActionInfo actionInfo)
     : base(String.Format("Mapping for {0}.{1} with {2} parameters not found. Make sure you have your Conventions correctly in place",
     actionInfo.Controller, actionInfo.Name, actionInfo.Parameters != null ? actionInfo.Parameters.Count : 0))
 {
     ActionInfo = actionInfo;
 }
 public string GetRouteName(ActionInfo action)
 {
     return StringHelpers.GetUniqueString(action.Controller, action.Name);
 }
 public string GetRouteController(ActionInfo action)
 {
     return action.Controller;
 }
        public HttpVerbs GetRouteConstraint(ActionInfo action)
        {
            var mapping = GetMappingByNameAndParameters(action);

            return mapping.Verb;
        }
 public string GetRouteAction(ActionInfo action)
 {
     return action.Name;
 }
        RouteMapping GetMappingByNameAndParameters(ActionInfo action)
        {
            // TODO: Refactor this to simplify it.
            var mappings = (from mapping in _routeConventions.Mappings
                           where mapping.ActionName == action.Name
                           select mapping).ToList();

            if (mappings.Count == 0)
                throw new MappingException(action);

            if (mappings.Count() == 1)
            {
                return mappings.First();
            }

            if (mappings.Count() > 1)
            {
                if (action.Parameters == null || action.Parameters.Count == 0)
                {
                    var noParamsMappings = (from mapping in mappings
                           where mapping.ActionParams == null
                           select mapping).ToList();

                    if (noParamsMappings.Count == 0)
                    {
                        throw new MappingException(action);
                    }
                    return noParamsMappings.First();
                }
                var equalParamsMappings = (from mapping in mappings
                                           where mapping.ActionParams != null && mapping.ActionParams.Count == action.Parameters.Count
                                           select mapping).ToList();

                foreach (var mapping in equalParamsMappings)
                {
                    var equalParams = 0;

                    foreach (var found in
                        mapping.ActionParams.Select(mappingParam => action.Parameters.Any(actionParam => mappingParam != actionParam)).Where(found => found))
                    {
                        equalParams++;
                        continue;
                    }
                    if (mapping.ActionParams.Count == equalParams)
                    {
                        return mapping;
                    }

                }
            }
            throw new MappingException(action);
        }
Beispiel #9
0
        public string GetRouteUrl(ActionInfo action)
        {
            if (action.Name.Equals(ResourceListAction, StringComparison.CurrentCultureIgnoreCase) &&
                MapResourceListToPluralController)
            {
                return StringHelpers.Pluralize(action.Controller);
            }

            return String.Format("{0}{1}", action.Controller,
                                 action.Parameters.Where(p => !p.IsComplexType).Aggregate(String.Empty,
                                                                                          (current, actionParam) =>
                                                                                          current + "/{" +
                                                                                          actionParam.Name + "}"));
        }
Beispiel #10
0
 public HttpVerbs GetRouteConstraint(ActionInfo action)
 {
     return action.Verb;
 }