From 4e1511ed4d3610374667f2aae5d2a589ed76c51f Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Fri, 4 Apr 2014 14:55:00 -0700 Subject: [PATCH] Adding ActionLink --- .../MvcSample.Web/Views/Shared/MyView.cshtml | 2 +- .../Rendering/Html/HtmlHelper.cs | 31 ++++++- .../Rendering/Html/HtmlHelperOfT.cs | 1 - .../Rendering/HtmlHelperLinkExtensions.cs | 93 +++++++++++++++++++ .../Rendering/IHtmlHelperOfT.cs | 32 +++++++ 5 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 src/Microsoft.AspNet.Mvc.Core/Rendering/HtmlHelperLinkExtensions.cs diff --git a/samples/MvcSample.Web/Views/Shared/MyView.cshtml b/samples/MvcSample.Web/Views/Shared/MyView.cshtml index 9964d7cdce..93bbfee5cf 100644 --- a/samples/MvcSample.Web/Views/Shared/MyView.cshtml +++ b/samples/MvcSample.Web/Views/Shared/MyView.cshtml @@ -68,7 +68,7 @@

Go to our cool travel reservation system.

-

Reserve Now

+

@Html.ActionLink("Reserve Now", "Fly", "Flight", new { area = "Travel" }, new { @class = "btn btn-default" })

Get more libraries

diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs index ca84bf991a..51e75dfa11 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Globalization; using System.IO; -using System.Linq.Expressions; using System.Net; using System.Reflection; using System.Text; @@ -88,6 +87,20 @@ namespace Microsoft.AspNet.Mvc.Rendering protected IModelMetadataProvider MetadataProvider { get; private set; } + public HtmlString ActionLink( + [NotNull] string linkText, + string actionName, + string controllerName, + string protocol, + string hostname, + string fragment, + object routeValues, + object htmlAttributes) + { + var url = _urlHelper.Action(actionName, controllerName, routeValues); + return GenerateLink(linkText, url, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)); + } + /// /// Creates a dictionary from an object, by adding each public instance property as a key with its associated /// value to the dictionary. It will expose public properties from derived types as well. This is typically used @@ -483,6 +496,22 @@ namespace Microsoft.AspNet.Mvc.Rendering return theForm; } + protected virtual HtmlString GenerateLink( + [NotNull] string linkText, + [NotNull] string url, + IDictionary htmlAttributes) + { + var tagBuilder = new TagBuilder("a") + { + InnerHtml = WebUtility.HtmlEncode(linkText), + }; + + tagBuilder.MergeAttributes(htmlAttributes); + tagBuilder.MergeAttribute("href", url); + + return tagBuilder.ToHtmlString(TagRenderMode.Normal); + } + protected virtual HtmlString GenerateTextBox(ModelMetadata metadata, string name, object value, string format, IDictionary htmlAttributes) { diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelperOfT.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelperOfT.cs index 9fd01c2d98..f802c84ca0 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelperOfT.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelperOfT.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics.Contracts; using System.Linq.Expressions; using Microsoft.AspNet.Mvc.Core; using Microsoft.AspNet.Mvc.ModelBinding; diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/HtmlHelperLinkExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/HtmlHelperLinkExtensions.cs new file mode 100644 index 0000000000..301dab4c91 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/HtmlHelperLinkExtensions.cs @@ -0,0 +1,93 @@ + +namespace Microsoft.AspNet.Mvc.Rendering +{ + public static class HtmlHelperLinkExtensions + { + public static HtmlString ActionLink( + [NotNull] this IHtmlHelper helper, + [NotNull] string linkText, + string actionName) + { + return helper.ActionLink( + linkText, + actionName, + controllerName: null, + protocol: null, + hostname: null, + fragment: null, + routeValues: null, + htmlAttributes: null); + } + + public static HtmlString ActionLink( + [NotNull] this IHtmlHelper helper, + [NotNull] string linkText, + string actionName, + object routeValues) + { + return helper.ActionLink( + linkText, + actionName, + controllerName: null, + protocol: null, + hostname: null, + fragment: null, + routeValues: routeValues, + htmlAttributes: null); + } + + public static HtmlString ActionLink( + [NotNull] this IHtmlHelper helper, + [NotNull] string linkText, + string actionName, + object routeValues, + object htmlAttributes) + { + return helper.ActionLink( + linkText, + actionName, + controllerName: null, + protocol: null, + hostname: null, + fragment: null, + routeValues: routeValues, + htmlAttributes: htmlAttributes); + } + + public static HtmlString ActionLink( + [NotNull] this IHtmlHelper helper, + [NotNull] string linkText, + string actionName, + string controllerName) + { + return helper.ActionLink( + linkText, + actionName, + controllerName, + protocol: null, + hostname: null, + fragment: null, + routeValues: null, + htmlAttributes: null); + } + + public static HtmlString ActionLink( + [NotNull] this IHtmlHelper helper, + [NotNull] string linkText, + string actionName, + string controllerName, + object routeValues, + object htmlAttributes) + { + return helper.ActionLink( + linkText, + actionName, + controllerName, + protocol: null, + hostname: null, + fragment: null, + routeValues: routeValues, + htmlAttributes: htmlAttributes); + } + } +} diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/IHtmlHelperOfT.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/IHtmlHelperOfT.cs index b00ec094b6..44928af7e4 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Rendering/IHtmlHelperOfT.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/IHtmlHelperOfT.cs @@ -31,6 +31,38 @@ namespace Microsoft.AspNet.Mvc.Rendering /// ViewDataDictionary ViewData { get; } + /// + /// Returns an anchor element (a element) that contains a url path to the specified action. + /// + /// The inner text of the anchor element. + /// The name of the action. + /// The name of the controller. + /// The protocol for the URL, such as "http" or "https". + /// The host name for the URL. + /// The URL fragment name (the anchor name). + /// + /// An object that contains the parameters for a route. The parameters are retrieved through reflection by + /// examining the properties of the object. This object is typically created using object initializer syntax. + /// Alternatively, an instance containing the route parameters. + /// + /// + /// An object that contains the HTML attributes to set for the element. Alternatively, an + /// instance containing the HTML attributes. + /// + /// + /// An anchor element (a element). + /// + HtmlString ActionLink( + [NotNull] string linkText, + string actionName, + string controllerName, + string protocol, + string hostname, + string fragment, + object routeValues, + object htmlAttributes); + + /// /// Writes an opening
tag to the response. When the user submits the form, /// the request will be processed by an action method.