我目前已经创建了一个扩展方法
using System.Web;
using System.Web.Mvc;
namespace Mobi.QualityControl.Site.Infrastructure
{
public static class HtmlHelpers
{
public static HtmlString ActionImage(this HtmlHelper htmlHelper, string action, object routeValues, string imagePath, string alt)
{
var url = new UrlHelper(htmlHelper.ViewContext.RequestContext);
// build the <img> tag
var imgBuilder = new TagBuilder("img");
imgBuilder.MergeAttribute("src", url.Content(imagePath));
imgBuilder.MergeAttribute("alt", alt);
string imgHtml = imgBuilder.ToString(TagRenderMode.SelfClosing);
// build the <a> tag
var anchorBuilder = new TagBuilder("a");
anchorBuilder.MergeAttribute("href", url.Action(action, routeValues));
anchorBuilder.InnerHtml = imgHtml; // include the <img> tag inside
string anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal);
return new HtmlString(anchorHtml);
}
}
}
然后,我尝试在我的页面中使用它,并在我的 web.config 中使用 using 语句,它正在选择该方法,但它
@HtmlHelpers.ActionImage("账户/注册", "/images/home/gettingstarted.png", "编辑", "GettingStarted")
它说它仍然需要 5 个参数而不是 4 个。
这可能是非常简单的事情。任何帮助,将不胜感激。