Asp.net-Mvc

是否有用於圖像連結的 ASP.NET MVC HtmlHelper?

  • June 13, 2018

Html.RouteLink() HtmlHelper 非常適合文本連結。但是連結圖像的最佳方式是什麼?

這是我的,它的核心功能是做一些重載

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();
}
<a href="<%=Url.RouteUrl(...)%>"><img src="..." alt="..." /></a>

引用自:https://stackoverflow.com/questions/675319