Exemplo n.º 1
0
        /// <summary>
        /// Returns an anchor element (a element) for the specified link text, action, controller, and route values.
        /// </summary>
        /// <param name="linkText">The inner text of the anchor element.</param>
        /// <param name="actionName">The name of the action.</param>
        /// <param name="controllerName">The name of the controller.</param>
        /// <param name="routeValues">An Object that contains the parameters for a route.</param>
        /// <returns>An anchor element (a element).</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public IEncodedString ActionLink(string linkText, string actionName, string controllerName, object routeValues)
        {
            var routerValuesDict = UtilHelper.ObjectToDictionary(routeValues);

            return(ActionLink(linkText, actionName, controllerName, routerValuesDict, null));
        }
Exemplo n.º 2
0
        /// <summary>
        /// {{actionLink 'linkText' 'action' 'controller' (object key=value) (object key=value) 'protocol' 'host'}}
        /// </summary>
        /// <return>
        /// <a href=protocol://host/controller/action?routeValues>linkText</a>
        /// </return>
        public void RegisterActionLink_Helper()
        {
            _hbsService.RegisterHelper("actionLink", (output, context, arguments) =>
            {
                if (arguments.Length < 3 || arguments.Length > 7)
                {
                    throw new HandlebarsException("{{actionLink}} helper must have at least 3 arguments with maximum 7 arguments");
                }

                var linkText              = arguments.At <string>(0);
                var actionName            = arguments.At <string>(1);
                var controllerName        = arguments.At <string>(2);
                var initialRouteValues    = arguments.Length > 3 ? arguments.At <Dictionary <string, object> >(3) : null;
                var initialHtmlAttributes = arguments.Length > 4 ? arguments.At <Dictionary <string, object> >(4) : null;
                var protocol              = arguments.Length > 5 ? arguments.At <string>(5) : null;
                var hostName              = arguments.Length > 6 ? arguments.At <string>(6) : null;

                if (string.IsNullOrWhiteSpace(actionName))
                {
                    throw missingParameterException("actionLink", "actionName");
                }

                if (string.IsNullOrWhiteSpace(controllerName))
                {
                    throw missingParameterException("actionLink", "controllerName");
                }

                var routeValues = CastDictionary(initialRouteValues);
                if (initialRouteValues != null && routeValues == null)
                {
                    throw new ArgumentException("Couldn't Cast RouteValues. @{actionLink}");
                }

                var htmlAttributes = CastDictionary(initialHtmlAttributes);
                if (initialHtmlAttributes != null && htmlAttributes == null)
                {
                    throw new ArgumentException("Couldn't Cast HtmlAttributes. @{actionLink}");
                }


                Uri uri = null;
                if (routeValues != null && routeValues.Count != 0)
                {
                    routeValues.Add("action", actionName);
                    routeValues.Add("controller", controllerName);

                    uri = UtilHelper.BuildURI(protocol ?? _viewSettings.Protocol, hostName ?? _viewSettings.Hostname, _viewSettings.UrlPattern, routeValues);
                }
                else
                {
                    var uriBuilder = new UriBuilder(protocol ?? _viewSettings.Protocol, hostName ?? _viewSettings.Hostname)
                    {
                        Path = controllerName + "/" + actionName
                    };
                    uri = uriBuilder.Uri;
                }

                if (linkText == null)
                {
                    linkText = uri.ToString();
                }

                var htmlAttrStr = UtilHelper.ConvertDictionaryToString(htmlAttributes);
                var link        = string.Format("<a {2}href=\"{0}\">{1}</a>", uri.ToString(), HttpUtility.HtmlEncode(linkText), htmlAttrStr);

                output.WriteSafeString(link);
            });
        }