【问题标题】:Is there an ASP.NET MVC HtmlHelper for image links?是否有用于图像链接的 ASP.NET MVC HtmlHelper?
【发布时间】:2009-03-23 21:33:14
【问题描述】:

Html.RouteLink() HtmlHelper 非常适合文本链接。但是链接图像的最佳方式是什么?

【问题讨论】:

    标签: asp.net-mvc routes html-helper


    【解决方案1】:
    <a href="<%=Url.RouteUrl(...)%>"><img src="..." alt="..." /></a>
    

    【讨论】:

    • +1,因为这是我在这里回答的许多人中使用的最快的方法。我只是在这个方法中指定了动作和控制器,它工作得很好。
    【解决方案2】:

    这是我的,它的核心函数做一些重载

    public static string ImageLink(this HtmlHelper htmlHelper, string imgSrc, string alt, string actionName, string controllerName, object routeValues, object htmlAttributes, object imgHtmlAttributes)
    {
        UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
        string imgtag = htmlHelper.Image(imgSrc, alt,imgHtmlAttributes);
        string url = urlHelper.Action(actionName, controllerName, routeValues);
    
        TagBuilder imglink = new TagBuilder("a");
        imglink.MergeAttribute("href", url);
        imglink.InnerHtml =imgtag;
        imglink.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);
    
        return imglink.ToString();
    }
    

    【讨论】:

    • 这不应该是 HtmlString 而不是字符串吗? with return new HtmlString(imglink.ToString()); ?
    • 只是指出 htmlHelper.Image() 不再在 MVC4 中。
    • 不能声明 htmlHelper.Image()
    【解决方案3】:

    这是我从上面的MiniScalope 答案中获得的更新版本。我正在使用 VS2010 和 ASP.Net MVC 2 预览版

            public static string ImageLink(this HtmlHelper htmlHelper, string imgSrc, string alt, string actionName, string controllerName, object routeValues, object htmlAttributes, object imgHtmlAttributes)
        {
            UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
            TagBuilder imgTag = new TagBuilder("img");
            imgTag.MergeAttribute("src", imgSrc);
            imgTag.MergeAttributes((IDictionary<string, string>) imgHtmlAttributes,true);
            string url = urlHelper.Action(actionName, controllerName, routeValues);
    
    
    
            TagBuilder imglink = new TagBuilder("a");
            imglink.MergeAttribute("href", url);
            imglink.InnerHtml = imgTag.ToString();
            imglink.MergeAttributes((IDictionary<string, string>)htmlAttributes, true);
    
            return imglink.ToString();
    
        }
    

    【讨论】:

      【解决方案4】:
      <%= Html.ActionLink(Html.Image(imageUrl, imageAlt), actionName, controllerName) %>
      

      可以,图像扩展来自期货程序集。 或者自己做扩展。

      【讨论】:

      • 似乎不起作用。 Html.ActionLink() 方法似乎对第一个 arg 进行 html 编码,因此所有尖括号等都被转义。
      【解决方案5】:

      创建您自己的辅助扩展。

      public static string Image(this HtmlHelper helper, string src, string alt)
      {
          TagBuilder tb = new TagBuilder("img");
          tb.Attributes.Add("src", helper.Encode(src));
          tb.Attributes.Add("alt", helper.Encode(alt));
          return tb.ToString(TagRenderMode.SelfClosing);
      }
      

      【讨论】:

      • 这不会创建图像操作链接。这不是扎克想要的吗?
      • 即使这不能直接回答问题,我真的很喜欢这种创建图像标签助手扩展的方法。谢谢:)
      【解决方案6】:

      我没有足够的勇气来添加评论,但这是对 MiniScalope's comment above:

      UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;

      我建议将它本身作为一个 HtmlHelper 扩展方法(并简化它),以便重用:

      private static UrlHelper Url(this HtmlHelper helper)
      { 
        return new UrlHelper(helper.ViewContext.RequestContext);
      }
      

      【讨论】:

        【解决方案7】:
        <%= Html.RouteLink("PLACEHOLDER", ...).Replace("PLACEHOLDER", "<img src=""..."" alt=""..."" />")%>
        

        【讨论】:

          【解决方案8】:

          此代码已在 mvc4 上测试...

              public static MvcHtmlString ImageLink(this HtmlHelper htmlHelper, string imgSrc, string alt, string actionName, string controllerName, object routeValues, object htmlAttributes, object imgHtmlAttributes)
              {
                  UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
                  var imgTag = new TagBuilder("img");
                  imgTag.MergeAttribute("src", imgSrc);
                  imgTag.MergeAttributes((IDictionary<string, string>)imgHtmlAttributes, true);
                  string url = urlHelper.Action(actionName, controllerName, routeValues);
          
          
          
                  var imglink = new TagBuilder("a");
                  imglink.MergeAttribute("href", url);
                  imglink.InnerHtml = imgTag.ToString();
                  imglink.MergeAttributes((IDictionary<string, string>)htmlAttributes, true);
          
                  return MvcHtmlString.Create(imglink.ToString());
          
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-03-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-25
            • 1970-01-01
            相关资源
            最近更新 更多