asp.net-mvc 是否有用于图像链接的 ASP.NET MVC HtmlHelper?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/675319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:26:31  来源:igfitidea点击:

Is there an ASP.NET MVC HtmlHelper for image links?

asp.net-mvcrouteshtml-helper

提问by Zack Peterson

The Html.RouteLink() HtmlHelper works great for text links. But what's the best way to link an image?

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

回答by Zack Peterson

<a href="<%=Url.RouteUrl(...)%>"><img src="..." alt="..." /></a>

回答by MiniScalope

Here is mine, it`s the core function make some overloads

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

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();
}

回答by Gabriel Mongeon

This is an updated version that I have from MiniScalopeanswer above. I'm using VS2010 and ASP.Net MVC 2 Preview

这是我从上面的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();

    }

回答by AndreasN

<%= Html.ActionLink(Html.Image(imageUrl, imageAlt), actionName, controllerName) %>

could work, the image extension is from the futures assembly. Or make your own extention.

可以工作,图像扩展名来自期货程序集。或者做你自己的扩展。

回答by Chad Moran

Create your own helper extension.

创建您自己的助手扩展。

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);
}

回答by Oskar Austegard

I don't have enough SO swagger to add a comment, but this is a comment on MiniScalope's comment above:

我没有足够的招摇来添加评论,但这是对 上面 MiniScalope 评论的评论

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

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

I would suggest making this an HtmlHelper extension method in itself (and simplify it), for reuse:

我建议使其本身成为 HtmlHelper 扩展方法(并简化它),以便重用:

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

回答by Zack Peterson

<%= Html.RouteLink("PLACEHOLDER", ...).Replace("PLACEHOLDER", "<img src=""..."" alt=""..."" />")%>

回答by user2461195

this code has been tested on mvc4...

此代码已在 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());

    }