自定义Image HtmlHelper

        public static void Image(this HtmlHelper helper, string src, string alt = null, object htmlAttributes = null)
        {
            System.Web.UI.HtmlTextWriter writer = new HtmlTextWriter(helper.ViewContext.HttpContext.Response.Output);
            System.Web.Mvc.TagBuilder tbImage = new TagBuilder("img");

            tbImage.MergeAttribute("src", src);
            tbImage.MergeAttribute("alt", alt);
            if (htmlAttributes != null)
            {
                tbImage.MergeAttributes<string, object>(new RouteValueDictionary(htmlAttributes));
            }

            writer.InnerWriter.Write(tbImage.ToString(TagRenderMode.SelfClosing));
        }
原文地址:https://www.cnblogs.com/freeliver54/p/3964536.html