PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/MvcFutures/Mvc/LinkExtensions.cs

https://bitbucket.org/markhneedham/aspnet-mvc
C# | 43 lines | 25 code | 4 blank | 14 comment | 0 complexity | ee32359d33568453dcf1afc13130905b MD5 | raw file
  1. namespace Microsoft.Web.Mvc {
  2. using System;
  3. using System.Linq.Expressions;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Web.Mvc.Html;
  7. using System.Web.Routing;
  8. using Microsoft.Web.Mvc.Internal;
  9. [AspNetHostingPermission(System.Security.Permissions.SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  10. public static class LinkExtensions {
  11. [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")]
  12. public static string BuildUrlFromExpression<TController>(this HtmlHelper helper, Expression<Action<TController>> action) where TController : Controller {
  13. return LinkBuilder.BuildUrlFromExpression<TController>(helper.ViewContext, helper.RouteCollection, action);
  14. }
  15. /// <summary>
  16. /// Creates an anchor tag based on the passed in controller type and method
  17. /// </summary>
  18. /// <typeparam name="TController">The Controller Type</typeparam>
  19. /// <param name="action">The Method to route to</param>
  20. /// <param name="linkText">The linked text to appear on the page</param>
  21. /// <returns>System.String</returns>
  22. [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")]
  23. public static string ActionLink<TController>(this HtmlHelper helper, Expression<Action<TController>> action, string linkText) where TController : Controller {
  24. return ActionLink<TController>(helper, action, linkText, null);
  25. }
  26. /// <summary>
  27. /// Creates an anchor tag based on the passed in controller type and method
  28. /// </summary>
  29. /// <typeparam name="TController">The Controller Type</typeparam>
  30. /// <param name="action">The Method to route to</param>
  31. /// <param name="linkText">The linked text to appear on the page</param>
  32. /// <returns>System.String</returns>
  33. [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")]
  34. public static string ActionLink<TController>(this HtmlHelper helper, Expression<Action<TController>> action, string linkText, object htmlAttributes) where TController : Controller {
  35. RouteValueDictionary routingValues = ExpressionHelper.GetRouteValuesFromExpression(action);
  36. return helper.RouteLink(linkText, routingValues, new RouteValueDictionary(htmlAttributes));
  37. }
  38. }
  39. }