/// <summary> /// Generates a fully qualified URL for an action method by using the specified action name, controller name, route values, protocol to use and host name. /// </summary> /// <param name="actionName">The name of the action.</param> /// <param name="controllerName">The name of the controller.</param> /// <param name="routeValues">Dictionary that contains the parameters for a route.</param> /// <param name="protocol">The protocol for the URL, such as "http" or "https".</param> /// <param name="hostName">The host name for the URL.</param> /// <returns>The fully qualified URL to an action method..</returns> /// <exception cref="ArgumentNullException"></exception> public IEncodedString Action(string actionName, string controllerName, IDictionary <string, string> routeValues, string protocol, string hostName) { if (string.IsNullOrWhiteSpace(actionName)) { throw new ArgumentNullException("actionName"); } if (string.IsNullOrWhiteSpace(controllerName)) { throw new ArgumentNullException("controllerName"); } //var uriBuilder = new UriBuilder(protocol ?? _viewSettings.Protocol, hostName ?? _viewSettings.Hostname); 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; } var link = uri.ToString(); return(new RawString(link)); }
/// <summary> /// {{url 'action' 'controller' (object area='Manager') 'protocol' 'host'}} /// routeValues, protocol and hostName are optional /// Default host name, protocol and url pattern are fetched from ViewSettings /// </summary> /// <return> /// Url as string /// </return> public void RegisterUrl_Helper() { _hbsService.RegisterHelper("url", (context, arguments) => { if (arguments.Length < 2 || arguments.Length > 5) { throw new HandlebarsException("{{url}} helper must have at least 2 arguments with maximum 5 arguments"); } var actionName = arguments[0].ToString(); var controllerName = arguments[1].ToString(); var initialRouteValues = arguments.Length > 2 ? arguments[2] as Dictionary <string, object> : null; var protocol = arguments.Length > 3 ? arguments[3].ToString() : null; var hostName = arguments.Length > 4 ? arguments[4].ToString() : null; if (string.IsNullOrWhiteSpace(actionName)) { throw missingParameterException("url", "actionName"); } if (string.IsNullOrWhiteSpace(controllerName)) { throw missingParameterException("url", "controllerName"); } var routeValues = CastDictionary(initialRouteValues); if (initialRouteValues != null && routeValues == null) { throw new ArgumentException("Couldn't Cast RouteValues. @{url}"); } 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; } var link = uri.ToString(); return(link); }); }
/// <summary> /// Returns an anchor element (a element) for the specified link text, action, controller, protocol, host name, route values as a route value dictionary, and HTML attributes as a dictionary. /// </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="protocol">The protocol for the URL, such as "http" or "https".</param> /// <param name="hostName">The host name for the URL.</param> /// <param name="routeValues">Dictionary that contains the parameters for a route.</param> /// <param name="htmlAttributes">Dictionary that contains the HTML attributes to set for the element.</param> /// <returns>An anchor element (a element).</returns> /// <exception cref="ArgumentNullException"></exception> public IEncodedString ActionLink(string linkText, string actionName, string controllerName, string protocol, string hostName, IDictionary <string, string> routeValues, IDictionary <string, string> htmlAttributes) { if (string.IsNullOrWhiteSpace(actionName)) { throw new ArgumentNullException("actionName"); } if (string.IsNullOrWhiteSpace(controllerName)) { throw new ArgumentNullException("controllerName"); } //var uriBuilder = new UriBuilder(protocol ?? _viewSettings.Protocol, hostName?? _viewSettings.Hostname); 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); return(new RawString(link)); }
/// <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); }); }