2010-12-13 114 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

相關問題