2010-12-13 89 views

回答

2

要在视图中显示的图像,你可以使用<img>标签:

<img src="http://someotherserver/path/to/some/image.png" alt="" /> 
+0

感谢您的反馈 – 2012-08-09 14:44:28

1

,或者你可以做一个小的HTML帮助:

public static MvcHtmlString Image(this HtmlHelper helper, 
          string url, 
          object htmlAttributes) 
{ 
    return Image(helper, url, null, htmlAttributes); 
} 
public static MvcHtmlString Image(this HtmlHelper helper, 
           string url, 
           string altText, 
           object htmlAttributes) 
{ 
    TagBuilder builder = new TagBuilder("image"); 

    var path = url.Split('?'); 
    string pathExtra = ""; 
    if(path.Length >1) 
    { 
     pathExtra = "?" + path[1]; 
    } 
    builder.Attributes.Add("src", VirtualPathUtility.ToAbsolute(path[0]) + pathExtra); 
    builder.Attributes.Add("alt", altText); 
    builder.MergeAttributes(new RouteValueDictionary(htmlAttributes)); 
    return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing)); 
} 

典型用法:

<%=Html.Image("~/content/images/ajax-loader.gif", new{style="margin: 0 auto;"})%> 

享受..

+0

非常感谢您的好回答 – 2012-08-09 14:43:59

相关问题