правильный способ указать SRC для изображения в MVC3 - PullRequest
4 голосов
/ 02 октября 2011
<img src="images/img2.jpg" alt="" title="" />

Вот как я это делал.

но верно ли это для Layout.cshtml и частичных представлений?Разве я не должен pecify источника, используя что-то вроде @ URl.

Я прошу, чтобы узнать, каков правильный путь

Ответы [ 2 ]

4 голосов
/ 02 октября 2011

Это, вероятно, сломается.Ниже приведен правильный способ сделать это:

<img src="@Url.Content("~/images/img2.jpg")" alt="" title="" />
1 голос
/ 02 октября 2011

Используйте вспомогательную функцию ImageExtensions из Mvc3Futures \ Microsoft.Web.Mvc.dll.

Вы можете найти его в NuGet хранилище .

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);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...