我只是寫了一些助手的圖像,你可以使用。 (1)只需創建一個名爲AppHelper的公共靜態類,然後將其添加到名爲'Helpers'的MVC項目中的文件夾中。然後將其添加到using System.Web.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方法。還應該強迫你一些很好的習慣所:喜歡使用替代文本等
對於腳本中使用: <script type="text/javascript" src="<% =Url.Content("~/Scripts/mootools.js") %>"></script>
是否運行了MVC的版本? RC1? RC1刷新? RC2? – 2009-03-06 22:01:39
我正在運行RC2。 – 2009-03-06 22:04:17