/// <summary>
        /// Determines whether the action method selection is valid for the specified
        /// controller context.
        /// </summary>
        /// <param name="controllerContext">The controller context.</param>
        /// <param name="methodInfo">Information about the action method.</param>
        /// <returns>
        /// true if the <see cref="ControllerContext.RouteData"/> has values for
        /// all parameters decorated with <see cref="FromRouteAttribute"/>, and if all keys
        /// in <see cref="ControllerContext.RouteData"/> match any of the decorated parameters,
        /// excluding controller, action and other route parameters that do not map to action method parameters.
        /// </returns>
        public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
        {
            var routeValues = new RouteValueDictionary(controllerContext.RouteData.Values);

            routeValues.Remove("controller");
            routeValues.Remove("action");

            CodeRoute codeRoute = controllerContext.RouteData.Route as CodeRoute;

            if (codeRoute != null)
            {
                for (int i = 0; i < codeRoute.NonActionParameterTokens.Count; i++)
                {
                    routeValues.Remove(codeRoute.NonActionParameterTokens[i]);
                }
            }

#pragma warning disable 0618

            string[] parameters = actionDataCache.GetOrAdd(methodInfo, (m) =>
                                                           (from p in m.GetParameters()
                                                            where p.IsDefined(typeof(MvcCodeRouting.FromRouteAttribute), inherit: true)
                                                            select p.Name).ToArray()
                                                           );

#pragma warning restore 0618

            return(parameters.All(p => routeValues.Keys.Contains(p, StringComparer.OrdinalIgnoreCase)) &&
                   routeValues.Keys.All(k => parameters.Contains(k, StringComparer.OrdinalIgnoreCase)));
        }
Ejemplo n.º 2
0
        protected override Type GetControllerType(RequestContext requestContext, string controllerName)
        {
            CodeRoute codeRoute = requestContext.RouteData.Route as CodeRoute;

            if (codeRoute == null ||
                codeRoute.ControllerTypes == null ||
                codeRoute.ControllerTypes.Count == 0)
            {
                return(base.GetControllerType(requestContext, controllerName));
            }

            Type controllerType;

            if (codeRoute.ControllerTypes.TryGetValue(controllerName, out controllerType))
            {
                return(controllerType);
            }

            return(null);
        }