/src/MvcFutures/Mvc/LinkExtensions.cs
C# | 43 lines | 25 code | 4 blank | 14 comment | 0 complexity | ee32359d33568453dcf1afc13130905b MD5 | raw file
- namespace Microsoft.Web.Mvc {
- using System;
- using System.Linq.Expressions;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Mvc.Html;
- using System.Web.Routing;
- using Microsoft.Web.Mvc.Internal;
-
- [AspNetHostingPermission(System.Security.Permissions.SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
- public static class LinkExtensions {
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1055:UriReturnValuesShouldNotBeStrings", Justification = "This is a UI method and is required to use strings as Uri"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an Extension Method which allows the user to provide a strongly-typed argument via Expression")]
- public static string BuildUrlFromExpression<TController>(this HtmlHelper helper, Expression<Action<TController>> action) where TController : Controller {
- return LinkBuilder.BuildUrlFromExpression<TController>(helper.ViewContext, helper.RouteCollection, action);
- }
-
- /// <summary>
- /// Creates an anchor tag based on the passed in controller type and method
- /// </summary>
- /// <typeparam name="TController">The Controller Type</typeparam>
- /// <param name="action">The Method to route to</param>
- /// <param name="linkText">The linked text to appear on the page</param>
- /// <returns>System.String</returns>
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an Extension Method which allows the user to provide a strongly-typed argument via Expression")]
- public static string ActionLink<TController>(this HtmlHelper helper, Expression<Action<TController>> action, string linkText) where TController : Controller {
- return ActionLink<TController>(helper, action, linkText, null);
- }
-
- /// <summary>
- /// Creates an anchor tag based on the passed in controller type and method
- /// </summary>
- /// <typeparam name="TController">The Controller Type</typeparam>
- /// <param name="action">The Method to route to</param>
- /// <param name="linkText">The linked text to appear on the page</param>
- /// <returns>System.String</returns>
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an Extension Method which allows the user to provide a strongly-typed argument via Expression")]
- public static string ActionLink<TController>(this HtmlHelper helper, Expression<Action<TController>> action, string linkText, object htmlAttributes) where TController : Controller {
- RouteValueDictionary routingValues = ExpressionHelper.GetRouteValuesFromExpression(action);
-
- return helper.RouteLink(linkText, routingValues, new RouteValueDictionary(htmlAttributes));
- }
- }
- }