私はちょうど使用できるイメージのためのいくつかのヘルパーを書いています。
(1)単にusing System.Web.Mvc;
でAppHelperという静的静的クラスを作成し、これをMVCプロジェクトの「ヘルパー」というフォルダに追加するだけです。
(2)これらの方法でコピー:
public static string Image(this HtmlHelper helper,
string classText, string sourcePath, string altText, string width, string height)
{
return Image(helper, classText, sourcePath, altText, width, height, null);
}
public static string Image(this HtmlHelper helper,
string classText, string sourcePath, string altText, string width, string height, object htmlAttributes)
{
StringBuilder sb = new StringBuilder();
if (htmlAttributes != null)
foreach (PropertyInfo p in htmlAttributes.GetType().GetProperties())
sb.AppendFormat(@" {0}=""{1}""", p.Name, p.GetValue(htmlAttributes, null).ToString());
if (htmlAttributes == null)
return String.Format(@"<img{0} src=""{1}"" alt=""{2}"" width=""{3}"" height=""{4}"" />",
String.IsNullOrEmpty(classText) ? String.Empty : String.Format(@" class=""{0}""", classText),
(new UrlHelper(helper.ViewContext.RequestContext)).Content(sourcePath),
altText, width, height);
else
return String.Format(@"<img{0} src=""{1}"" alt=""{2}"" width=""{3}"" height=""{4}""{5} />",
String.IsNullOrEmpty(classText) ? String.Empty : String.Format(@" class=""{0}""", classText),
(new UrlHelper(helper.ViewContext.RequestContext)).Content(sourcePath),
altText, width, height, sb.ToString());
}
(3).. <% =Html.Image("small_pic_border","~/Content/Images/Home/office2_137x139.jpg","principal headshot","137","139") %>
このメソッドは、liammclennanが指摘したUrl.Contentメソッドを使用します。また、いくつかの良いhabbitsにあなたを強制する必要があります。スクリプトの場合など
代替テキストを使用してのように使用します。あなたは <script type="text/javascript" src="<% =Url.Content("~/Scripts/mootools.js") %>"></script>
を実行しているMVCのどのバージョン? RC1? RC1リフレッシュ? RC2? –
私はRC2を実行しています。 –