我想创建一个图片链接,但看不到图片,这是我的扩展方法:
public static MvcHtmlString ActionImage(this HtmlHelper htmlHelper,
string controller,
string action,
object routeValues,
string imagePath,
string alternateText = "",
object htmlAttributes = null)
{
var anchorBuilder = new TagBuilder("a");
var url = new UrlHelper(htmlHelper.ViewContext.RequestContext);
anchorBuilder.MergeAttribute("href", url.Action(action, controller, routeValues));
var imgBuilder = new TagBuilder("img");
imgBuilder.MergeAttribute("src", url.Content(imagePath));
imgBuilder.MergeAttribute("alt", alternateText);
var attributes = (IDictionary<string, object>)HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
imgBuilder.MergeAttributes(attributes);
string imgHtml = imgBuilder.ToString(TagRenderMode.SelfClosing);
anchorBuilder.InnerHtml = imgHtml;
return MvcHtmlString.Create(anchorBuilder.ToString());
}
我在我看来使用它:
@foreach (var item in Model)
{
.
.
<div>
Image:
@Html.ActionImage("ControllerName", "action",
"routeValues", item.ImageAddress.toString())
</div>
.
.
}
ImageAddress 是字符串但不起作用,当我使用特定地址时没有区别
@Html.ActionImage("ControllerName", "action",
"routeValues", "C:\1.jpg")
仅当使用此地址时它才能正常工作:
"../../Content/Images/1.jpg"
有什么问题?谢谢你的帮助,对不起我的英语