Exemple #1
0
        /// <summary>
        /// Convenience method used to generate a link
        /// using Routing to determine the virtual path.
        /// </summary>
        /// <param name="routeKey">The name of the route, if any.</param>
        /// <param name="action">The action with parameters.</param>
        /// <typeparam name="TController">The type of the controller.</typeparam>
        public virtual string Route <TController>(string routeKey,
                                                  Expression <Action <TController> > action)
            where TController : IController
        {
            Precondition.Require(action, () => Error.ArgumentNull("action"));

            MethodCallExpression mexp = (action.Body as MethodCallExpression);

            if (mexp == null)
            {
                throw Error.ExpressionMustBeAMethodCall("action");
            }

            if (mexp.Object != action.Parameters[0])
            {
                throw Error.MethodCallMustTargetLambdaArgument("action");
            }

            string actionName     = ActionMethodSelector.GetNameOrAlias(mexp.Method);
            string controllerName = typeof(TController).Name;

            ValueDictionary rvd = LinqHelper.ExtractArgumentsToDictionary(mexp);

            rvd = (rvd != null) ? rvd : new ValueDictionary();

            return(HttpUtility.HtmlAttributeEncode(GenerateUrl(routeKey, controllerName, actionName, rvd)));
        }
        public static Route MapAsyncRoute <TController>(this RouteCollection routes,
                                                        string key, string url, Expression <Action <TController> > action,
                                                        params IRouteConstraint[] constraints)
            where TController : IAsyncController
        {
            Precondition.Require(routes, () => Error.ArgumentNull("routes"));
            Precondition.Require(url, () => Error.ArgumentNull("url"));

            MethodCallExpression mexp = (action.Body as MethodCallExpression);

            if (mexp == null)
            {
                throw Error.ExpressionMustBeAMethodCall("action");
            }

            if (mexp.Object != action.Parameters[0])
            {
                throw Error.MethodCallMustTargetLambdaArgument("action");
            }

            string actionName     = ActionMethodSelector.GetNameOrAlias(mexp.Method);
            string controllerName = typeof(TController).Name;

            ValueDictionary defaults = LinqHelper.ExtractArgumentsToDictionary(mexp);

            if (defaults == null)
            {
                defaults = new ValueDictionary();
            }

            defaults["controller"] = controllerName;
            defaults["action"]     = actionName;

            Route route = (constraints == null) ?
                          new Route(url, defaults, constraints, new MvcAsyncRouteHandler()) :
                          new Route(url, defaults, new MvcAsyncRouteHandler());

            routes.Add(key, route);
            return(route);
        }