using System;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcTest2.Helpers
{
public static class ImageHelpers
{
public static MvcHtmlString ImgLink(this HtmlHelper helper,string actionName,string imgUrl,string alternateText,object routeValues,object linkHtmlAttributes,object imgHtmlAttributes)
{
var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);//在哪一个页面引用的就获取这个页面的url帮助实体类
var url = urlHelper.Action(actionName,routeValues);
var linkTagBuilder = new TagBuilder("a");//使用这个方法生成标签
linkTagBuilder.MergeAttribute("href",url);
linkTagBuilder.MergeAttributes(new RouteValueDictionary(linkHtmlAttributes));
var imgTagBuilder = new TagBuilder("img");
imgTagBuilder.MergeAttribute("src",imgUrl);
imgTagBuilder.MergeAttribute("alt", alternateText);
imgTagBuilder.MergeAttribute("title", alternateText);
imgTagBuilder.MergeAttributes(new RouteValueDictionary(imgHtmlAttributes));
linkTagBuilder.InnerHtml = imgTagBuilder.ToString(TagRenderMode.SelfClosing);//转换成自身标记结束的标签
return MvcHtmlString.Create(linkTagBuilder.ToString());
}
}
方法返回一个linkImg