Html.RouteLink() HtmlHelper 非常适合文本链接。但是链接图像的最佳方式是什么?
Zack
问问题
33301 次
8 回答
34
<a href="<%=Url.RouteUrl(...)%>"><img src="..." alt="..." /></a>
于 2009-03-23T21:34:53.867 回答
24
这是我的,它的核心功能是做一些重载
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();
}
于 2009-08-04T12:41:55.893 回答
15
这是我从上面的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();
}
于 2010-01-25T03:48:55.737 回答
9
<%= Html.ActionLink(Html.Image(imageUrl, imageAlt), actionName, controllerName) %>
可以工作,图像扩展来自期货大会。或者自己做扩展。
于 2009-03-23T21:40:30.497 回答
7
创建您自己的辅助扩展。
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);
}
于 2009-03-23T21:36:15.577 回答
5
我没有足够的自信来添加评论,但这是对 上面 MiniScalope 评论的评论:
UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;
我建议将其作为 HtmlHelper 扩展方法本身(并简化它),以便重用:
private static UrlHelper Url(this HtmlHelper helper)
{
return new UrlHelper(helper.ViewContext.RequestContext);
}
于 2009-10-22T15:43:15.367 回答
3
<%= Html.RouteLink("PLACEHOLDER", ...).Replace("PLACEHOLDER", "<img src=""..."" alt=""..."" />")%>
于 2009-03-23T21:41:09.340 回答
2
此代码已在 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());
}
于 2013-06-24T23:37:10.877 回答