Ejemplo n.º 1
0
 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))});
 }
Ejemplo n.º 2
0
        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();
            }
        }
Ejemplo n.º 3
0
 public string GetRouteName(ActionInfo action)
 {
     return StringHelpers.GetUniqueString(action.Controller, action.Name);
 }
Ejemplo n.º 4
0
 public string GetRouteController(ActionInfo action)
 {
     return action.Controller;
 }
Ejemplo n.º 5
0
        public HttpVerbs GetRouteConstraint(ActionInfo action)
        {
            var mapping = GetMappingByNameAndParameters(action);

            return mapping.Verb;
        }
Ejemplo n.º 6
0
 public string GetRouteAction(ActionInfo action)
 {
     return action.Name;
 }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
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 + "}"));
        }
Ejemplo n.º 9
0
 public HttpVerbs GetRouteConstraint(ActionInfo action)
 {
     return action.Verb;
 }