我使用下面的生成操作鏈接:
using System;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
using Fasterflect;
namespace StackOverflow.Mvc.Extensions
{
public static class HtmlExtensions
{
#region ActionImage
// href image link
public static string ActionImage(this HtmlHelper helper, string href, string linkText, object htmlAttributes,
string alternateText, string imageSrc, object imageAttributes)
{
var sb = new StringBuilder();
const string format = "<a href=\"{0}\"{1}>{2}</a>";
string image = helper.Image(imageSrc, alternateText, imageAttributes).ToString();
string content = string.IsNullOrWhiteSpace(linkText) ? image : image + linkText;
sb.AppendFormat(format, href, GetAttributeString(htmlAttributes), content);
return sb.ToString();
}
// controller/action image link
public static string ActionImage(this HtmlHelper helper, string controller, string action, string linkText, object htmlAttributes,
string alternateText, string imageSrc, object imageAttributes)
{
bool isDefaultAction = string.IsNullOrEmpty(action) || action == "Index";
string href = "/" + (controller ?? "Home") + (isDefaultAction ? string.Empty : "/" + action);
return ActionImage(helper, href, linkText, htmlAttributes, alternateText, imageSrc, imageAttributes);
}
// T4MVC ActionResult image link
public static string ActionImage(this HtmlHelper helper, ActionResult actionResult, string linkText, object htmlAttributes,
string alternateText, string imageSrc, object imageAttributes)
{
var controller = (string) actionResult.GetPropertyValue("Controller");
var action = (string) actionResult.GetPropertyValue("Action");
return ActionImage(helper, controller, action, linkText, htmlAttributes, alternateText, imageSrc, imageAttributes);
}
#endregion
#region Helpers
private static string GetAttributeString(object htmlAttributes)
{
if(htmlAttributes == null)
{
return string.Empty;
}
const string format = " {0}=\"{1}\"";
var sb = new StringBuilder();
htmlAttributes.GetType().Properties().ForEach(p => sb.AppendFormat(format, p.Name, p.Get(htmlAttributes)));
return sb.ToString();
}
#endregion
}
}
注意,GetAttributeString方法依賴於Fasterflect庫,使反射的任務更容易,但如果你喜歡不採取額外的依賴,你可以替換成正反射。
圖像幫助器擴展名曾經是MvcContrib的一部分,但似乎已被刪除,很可能是因爲該功能現在內置於MVC中。無論如何,我已經在下面列出了完整性:
public static class ImageExtensions {
public static MvcHtmlString Image(this HtmlHelper helper, string imageRelativeUrl, string alt, object htmlAttributes) {
return Image(helper, imageRelativeUrl, alt, new RouteValueDictionary(htmlAttributes));
}
public static MvcHtmlString Image(this HtmlHelper helper, string imageRelativeUrl, string alt, IDictionary<string, object> htmlAttributes) {
if (String.IsNullOrEmpty(imageRelativeUrl)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "imageRelativeUrl");
}
string imageUrl = UrlHelper.GenerateContentUrl(imageRelativeUrl, helper.ViewContext.HttpContext);
return MvcHtmlString.Create(Image(imageUrl, alt, htmlAttributes).ToString(TagRenderMode.SelfClosing));
}
public static TagBuilder Image(string imageUrl, string alt, IDictionary<string, object> htmlAttributes) {
if (String.IsNullOrEmpty(imageUrl)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "imageUrl");
}
TagBuilder imageTag = new TagBuilder("img");
if (!String.IsNullOrEmpty(imageUrl)) {
imageTag.MergeAttribute("src", imageUrl);
}
if (!String.IsNullOrEmpty(alt)) {
imageTag.MergeAttribute("alt", alt);
}
imageTag.MergeAttributes(htmlAttributes, true);
if (imageTag.Attributes.ContainsKey("alt") && !imageTag.Attributes.ContainsKey("title")) {
imageTag.MergeAttribute("title", (imageTag.Attributes["alt"] ?? "").ToString());
}
return imageTag;
}
}
什麼是錯的您上面張貼的摘錄?它似乎沒有太多的粗俗,並且它具有以正常標準方式指定HTML屬性的優點。 (title,alt等)而且,無論如何,如果你願意的話,自己寫這樣一種擴展方法是非常容易的。 – 2011-02-24 22:11:34
@Kirk Woll現在我所用的東西沒有問題,但這不是問題。我只是試圖看看是否有更好的方法 – zam6ak 2011-02-24 22:19:44
自定義HTML助手解決方案有什麼問題? – 2011-02-25 07:20:06