Ryan
Ryan

Reputation: 5546

right way to specify the src for an image in MVC3

I have the following code:

<img src="images/img2.jpg" alt="" title="" />

Is this correct for Layout.cshtml and partial views?

Should I instead specify the source using something like @URl?

Upvotes: 4

Views: 222

Answers (2)

Jahan Zinedine
Jahan Zinedine

Reputation: 14874

Use the ImageExtensions Helper function from the Mvc3Futures\Microsoft.Web.Mvc.dll.

You can find it in 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);
}

Upvotes: 2

tugberk
tugberk

Reputation: 58444

This will probably gonna break. The below one is the right way of doing it :

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

Upvotes: 4

Related Questions