2011-10-02 75 views
4
<img src="images/img2.jpg" alt="" title="" /> 

這就是我這樣做的。正確的方法來指定MVC3中的圖像的src

但這是正確的Layout.cshtml和部分意見? 我不應該使用類似@URL的東西來源化它。

我問,才能知道正確的方法是什麼

回答

4

這將可能會斷裂。下面一個是做的正確方法:

<img src="@Url.Content("~/images/img2.jpg")" alt="" title="" /> 
+0

還可以查看t4mvc獲取強類型的內容鏈接。 –

1

從Mvc3Futures \ Microsoft.Web.Mvc.dll使用ImageExtensions助手功能。

你可以在NuGet repository找到它。

public static class ImageExtensions 
{ 
    public static MvcHtmlString Image(this HtmlHelper helper, string imageRelativeUrl); 

    public static MvcHtmlString Image(this HtmlHelper helper, string imageRelativeUrl, string alt); 

    public static MvcHtmlString Image(this HtmlHelper helper, string imageRelativeUrl, string alt, object htmlAttributes); 

    public static MvcHtmlString Image(this HtmlHelper helper, string imageRelativeUrl, object htmlAttributes); 

    public static MvcHtmlString Image(this HtmlHelper helper, string imageRelativeUrl, IDictionary<string, object> htmlAttributes); 

    public static MvcHtmlString Image(this HtmlHelper helper, string imageRelativeUrl, string alt, IDictionary<string, object> htmlAttributes); 

    public static TagBuilder Image(string imageUrl, string alt, IDictionary<string, object> htmlAttributes); 
}