PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/MvcFutures/Mvc/ImageExtensions.cs

https://bitbucket.org/markhneedham/aspnet-mvc
C# | 70 lines | 57 code | 13 blank | 0 comment | 6 complexity | 22b3eaedd698c52e4c87eb03099ee200 MD5 | raw file
  1. namespace Microsoft.Web.Mvc {
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Web.Routing;
  7. using Microsoft.Web.Mvc.Resources;
  8. [AspNetHostingPermission(System.Security.Permissions.SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  9. public static class ImageExtensions {
  10. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "The return value is not a regular URL since it may contain ~/ ASP.NET-specific characters")]
  11. public static string Image(this HtmlHelper helper, string imageRelativeUrl) {
  12. return Image(helper, imageRelativeUrl, null, (IDictionary<string, object>)null);
  13. }
  14. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "Required for Extension Method")]
  15. public static string Image(this HtmlHelper helper, string imageRelativeUrl, string alt) {
  16. return Image(helper, imageRelativeUrl, alt, (IDictionary<string, object>)null);
  17. }
  18. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "The return value is not a regular URL since it may contain ~/ ASP.NET-specific characters")]
  19. public static string Image(this HtmlHelper helper, string imageRelativeUrl, string alt, object htmlAttributes) {
  20. return Image(helper, imageRelativeUrl, alt, new RouteValueDictionary(htmlAttributes));
  21. }
  22. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "The return value is not a regular URL since it may contain ~/ ASP.NET-specific characters")]
  23. public static string Image(this HtmlHelper helper, string imageRelativeUrl, object htmlAttributes) {
  24. return Image(helper, imageRelativeUrl, null, new RouteValueDictionary(htmlAttributes));
  25. }
  26. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "The return value is not a regular URL since it may contain ~/ ASP.NET-specific characters")]
  27. public static string Image(this HtmlHelper helper, string imageRelativeUrl, IDictionary<string, object> htmlAttributes) {
  28. return Image(helper, imageRelativeUrl, null, htmlAttributes);
  29. }
  30. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "The return value is not a regular URL since it may contain ~/ ASP.NET-specific characters")]
  31. public static string Image(this HtmlHelper helper, string imageRelativeUrl, string alt, IDictionary<string, object> htmlAttributes) {
  32. if (String.IsNullOrEmpty(imageRelativeUrl)) {
  33. throw new ArgumentException(MvcResources.Common_NullOrEmpty, "imageRelativeUrl");
  34. }
  35. UrlHelper url = new UrlHelper(helper.ViewContext);
  36. string imageUrl = url.Content(imageRelativeUrl);
  37. return Image(imageUrl, alt, htmlAttributes).ToString(TagRenderMode.SelfClosing);
  38. }
  39. public static TagBuilder Image(string imageUrl, string alt, IDictionary<string, object> htmlAttributes) {
  40. if (String.IsNullOrEmpty(imageUrl)) {
  41. throw new ArgumentException(MvcResources.Common_NullOrEmpty, "imageUrl");
  42. }
  43. TagBuilder imageTag = new TagBuilder("img");
  44. if (!String.IsNullOrEmpty(imageUrl)) {
  45. imageTag.MergeAttribute("src", imageUrl);
  46. }
  47. if (!String.IsNullOrEmpty(alt)) {
  48. imageTag.MergeAttribute("alt", alt);
  49. }
  50. imageTag.MergeAttributes(htmlAttributes, true);
  51. if (imageTag.Attributes.ContainsKey("alt") && !imageTag.Attributes.ContainsKey("title")) {
  52. imageTag.MergeAttribute("title", (imageTag.Attributes["alt"] ?? "").ToString());
  53. }
  54. return imageTag;
  55. }
  56. }
  57. }