PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/MVC/src/MvcFutures/Mvc/ImageExtensions.cs

#
C# | 68 lines | 55 code | 13 blank | 0 comment | 6 complexity | 766fc1b785568f5b04360c5ab7f40236 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. namespace Microsoft.Web.Mvc {
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Web.Mvc;
  5. using System.Web.Routing;
  6. using Microsoft.Web.Resources;
  7. public static class ImageExtensions {
  8. [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")]
  9. public static string Image(this HtmlHelper helper, string imageRelativeUrl) {
  10. return Image(helper, imageRelativeUrl, null, (IDictionary<string, object>)null);
  11. }
  12. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "Required for Extension Method")]
  13. public static string Image(this HtmlHelper helper, string imageRelativeUrl, string alt) {
  14. return Image(helper, imageRelativeUrl, alt, (IDictionary<string, object>)null);
  15. }
  16. [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")]
  17. public static string Image(this HtmlHelper helper, string imageRelativeUrl, string alt, object htmlAttributes) {
  18. return Image(helper, imageRelativeUrl, alt, new RouteValueDictionary(htmlAttributes));
  19. }
  20. [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")]
  21. public static string Image(this HtmlHelper helper, string imageRelativeUrl, object htmlAttributes) {
  22. return Image(helper, imageRelativeUrl, null, new RouteValueDictionary(htmlAttributes));
  23. }
  24. [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")]
  25. public static string Image(this HtmlHelper helper, string imageRelativeUrl, IDictionary<string, object> htmlAttributes) {
  26. return Image(helper, imageRelativeUrl, null, htmlAttributes);
  27. }
  28. [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")]
  29. public static string Image(this HtmlHelper helper, string imageRelativeUrl, string alt, IDictionary<string, object> htmlAttributes) {
  30. if (String.IsNullOrEmpty(imageRelativeUrl)) {
  31. throw new ArgumentException(MvcResources.Common_NullOrEmpty, "imageRelativeUrl");
  32. }
  33. UrlHelper url = new UrlHelper(helper.ViewContext.RequestContext);
  34. string imageUrl = url.Content(imageRelativeUrl);
  35. return Image(imageUrl, alt, htmlAttributes).ToString(TagRenderMode.SelfClosing);
  36. }
  37. public static TagBuilder Image(string imageUrl, string alt, IDictionary<string, object> htmlAttributes) {
  38. if (String.IsNullOrEmpty(imageUrl)) {
  39. throw new ArgumentException(MvcResources.Common_NullOrEmpty, "imageUrl");
  40. }
  41. TagBuilder imageTag = new TagBuilder("img");
  42. if (!String.IsNullOrEmpty(imageUrl)) {
  43. imageTag.MergeAttribute("src", imageUrl);
  44. }
  45. if (!String.IsNullOrEmpty(alt)) {
  46. imageTag.MergeAttribute("alt", alt);
  47. }
  48. imageTag.MergeAttributes(htmlAttributes, true);
  49. if (imageTag.Attributes.ContainsKey("alt") && !imageTag.Attributes.ContainsKey("title")) {
  50. imageTag.MergeAttribute("title", (imageTag.Attributes["alt"] ?? "").ToString());
  51. }
  52. return imageTag;
  53. }
  54. }
  55. }