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协助dao层
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

自定Html輔助方法必须写到静态类静态数据方法里边

 

在页面引用方法

@using  MvcTest2.Helpers

@Html.ImgLink()