ASP.NET MVC中ActionLink的图像等效物

在ASP.NET MVC中是否有相当于Img标签的Html.ActionLink助手?

我有一个控制器动作输出一个dynamic生成的JPEG,我想使用相同的Lambdaexpression式链接到它,因为我使用ActionLink做HREF。

或者,一个只给路由URL的帮助器(再次用Lambdas指定)也是可以接受的。

编辑 :我最初指定,我正在使用预览5,但是我看到一个testing版已经发布。 所以所有的版本号是一个不需要的信息,因为我可能会很快升级:-)

Url.Action()将为您提供Html.ActionLink的大部分重载的纯粹URL,但我认为URL-from-lambdafunction目前只能通过Html.ActionLink使用。 希望他们会在某个时候为Url.Action添加一个类似的重载。

您可以使用URL.Action方法

<a href="<%= Url.Action("Create") %>"><img src="../../Content/Images/add_48.png" /></a> 

这个问题比较老,我刚刚开始使用ASP.NET MVC,当RC已经出来时,但是对于后来发现这个问题的人来说,这可能是有趣的:

至less在RC中,你可以使用Url.Action()也可以使用匿名types,结果看起来比上面的build议更好,我猜:

 <a href="<%= Url.RouteUrl("MyRoute", new { param1 = "bla", param2 = 5 }) %>"> put in <span>whatever</span> you want, also <img src="a.gif" alt="images" />. </a> 

当然,还有许多其他的RouteUrl重载。

我使用了一种解决方法来为ActionLink放置一个标记而不是文本,然后将其replace为我的图像代码。 像这样的东西:

 <%= Html.ActionLink("__IMAGE_PLACEHOLDER__", "Products").Replace("__IMAGE_PLACEHOLDER__", "<img src=\"" + myImgUrl + "\" />")%> 

不是最优雅的解决scheme,但它的作品。

在MVC3中,你的链接看起来像这样:

 <a href="@Url.Action("Create")"><img src="../../Content/Images/add_48.png" /></a> 

在ASP.NET MVC Beta中,您可以使用Futures程序集中的Html.BuildUrlFromExpression方法(它不包含在默认的ASP.NET MVC安装中,但可以从CodePlex中获得 )来创build一个图像链接HTML – 使用lambda样式的ActionLink语法,如下所示:

 <a href="<%=Html.BuildUrlFromExpression<MyController>(c => c.MyAction())%>"> <%=Html.Image("~/Content/MyImage.gif")%> </a> 

为了保持你的图像链接无边界,你需要添加一个像这样的CSS规则:

 img { border: none; } 

你可以使用这个控件。它的行为就像ActionLink。

http://agilefutures.com/index.php/2009/06/actionimage-aspnet-mvc

在MVC 2中实现起来相当简单。我创build了自己的非常简单的扩展方法来支持Url.Action助手的Lambdaexpression式。 它要求你参考MVC 2期货。
代码如下:

 using System; using System.Linq.Expressions; using System.Web.Mvc; using System.Web.Routing; using ExpressionHelperInternal=Microsoft.Web.Mvc.Internal.ExpressionHelper; namespace Bnv.Bssi.Web.Infrastructure.Helpers { public static class UrlExtensions { public static string Action<TController>(this UrlHelper helper, Expression<Action<TController>> action) where TController : Controller { RouteValueDictionary routeValuesFromExpression = ExpressionHelperInternal.GetRouteValuesFromExpression<TController>(action); return helper.Action(routeValuesFromExpression["action"].ToString(), routeValuesFromExpression); } } } 

这是你如何使用它:

 <img src="<%= Url.Action<YourController>(c => c.YourActionMethod(param1, param2)); %>" /> 

我知道我的post太晚了,但我想分享:)

我添加了新的扩展方法是这样的:

 public static class ImageExtensions { public static MvcHtmlString ImageLink(this HtmlHelper htmlHelper, string imgSrc, string additionalText = null, string actionName = null, string controllerName = null, object routeValues = null, object linkHtmlAttributes = null, object imgHtmlAttributes = null) { var urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url; var url = "#"; if (!string.IsNullOrEmpty(actionName)) url = urlHelper.Action(actionName, controllerName, routeValues); var imglink = new TagBuilder("a"); imglink.MergeAttribute("href", url); imglink.InnerHtml = htmlHelper.Image(imgSrc, imgHtmlAttributes) + " " + additionalText; linkHtmlAttributes = new RouteValueDictionary(linkHtmlAttributes); imglink.MergeAttributes((IDictionary<string, object>)linkHtmlAttributes, true); return MvcHtmlString.Create(imglink.ToString()); } public static MvcHtmlString Image(this HtmlHelper htmlHelper, string imgSrc, object imgHtmlAttributes = null) { var imgTag = new TagBuilder("img"); imgTag.MergeAttribute("src", imgSrc); if (imgHtmlAttributes != null) { imgHtmlAttributes = new RouteValueDictionary(imgHtmlAttributes); imgTag.MergeAttributes((IDictionary<string, object>)imgHtmlAttributes, true); } return MvcHtmlString.Create(imgTag.ToString()); } } 

希望这有助于。

是Url.Content()你在找什么?

给它像Url.Content(“〜/ path / to / something.jpg”),它将把它变成基于应用程序根的适当path。

-Josh

我采取了上面的答案,并作了一些包装扩展:

  public static MvcHtmlString ActionImageLink(this HtmlHelper helper, string src, string altText, UrlHelper url, string actionName, string controllerName) { return ActionImageLink(helper, src, altText, url, actionName, controllerName, null, null); } public static MvcHtmlString ActionImageLink(this HtmlHelper helper, string src, string altText, UrlHelper url, string actionName, string controllerName, Dictionary<string, string> linkAttributes, Dictionary<string, string> imageAttributes) { return ActionImageLink(helper, src, altText, url, actionName, controllerName, null, linkAttributes, imageAttributes); } public static MvcHtmlString ActionImageLink(this HtmlHelper helper, string src, string altText, UrlHelper url, string actionName, string controllerName, dynamic routeValues, Dictionary<string, string> linkAttributes, Dictionary<string, string> imageAttributes) { var linkBuilder = new TagBuilder("a"); linkBuilder.MergeAttribute("href", routeValues == null ? url.Action(actionName, controllerName) : url.Action(actionName, controllerName, routeValues)); var imageBuilder = new TagBuilder("img"); imageBuilder.MergeAttribute("src", url.Content(src)); imageBuilder.MergeAttribute("alt", altText); if (linkAttributes != null) { foreach (KeyValuePair<string, string> attribute in linkAttributes) { if (!string.IsNullOrWhiteSpace(attribute.Key) && !string.IsNullOrWhiteSpace(attribute.Value)) { linkBuilder.MergeAttribute(attribute.Key, attribute.Value); } } } if (imageAttributes != null) { foreach (KeyValuePair<string, string> attribute in imageAttributes) { if (!string.IsNullOrWhiteSpace(attribute.Key) && !string.IsNullOrWhiteSpace(attribute.Value)) { imageBuilder.MergeAttribute(attribute.Key, attribute.Value); } } } linkBuilder.InnerHtml = MvcHtmlString.Create(imageBuilder.ToString(TagRenderMode.SelfClosing)).ToString(); return MvcHtmlString.Create(linkBuilder.ToString()); } 

无论如何,让我更容易,希望它帮助别人。

我试图把Html.Image的输出放到我的Html.ImageLink帮助器中。

 @(new HtmlString(Html.ActionLink(Html.Image("image.gif").ToString(), "myAction", "MyController").ToString().Replace("&lt;", "<").Replace("&gt;", ">"))) 

对我来说问题是,ActionLink名称是编码的,所以我有&lt而不是<。

我刚刚删除了这个编码,结果对我有用。 (有没有更好的方式来做这个,而不是使用replace?)

添加到其他post:在我的情况下(asp.net mvc 3)我想要一个图像链接作为一个语言select器,所以我结束了:

  public static MvcHtmlString ImageLink(this HtmlHelper htmlHelper, string imgSrc, string cultureName, object htmlAttributes, object imgHtmlAttributes, string languageRouteName = "lang", bool strictSelected = false) { UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url; TagBuilder imgTag = new TagBuilder("img"); imgTag.MergeAttribute("src", imgSrc); imgTag.MergeAttributes((IDictionary<string, string>)imgHtmlAttributes, true); var language = htmlHelper.LanguageUrl(cultureName, languageRouteName, strictSelected); string url = language.Url; TagBuilder imglink = new TagBuilder("a"); imglink.MergeAttribute("href", url); imglink.InnerHtml = imgTag.ToString(); imglink.MergeAttributes((IDictionary<string, string>)htmlAttributes, true); //if the current page already contains the language parameter make sure the corresponding html element is marked string currentLanguage = htmlHelper.ViewContext.RouteData.GetRequiredString("lang"); if (cultureName.Equals(currentLanguage, StringComparison.InvariantCultureIgnoreCase)) { imglink.AddCssClass("selectedLanguage"); } return new MvcHtmlString(imglink.ToString()); } 

国际化的支持是通过语言路线完成的 – 这里是原始资料。

这里有很好的解决scheme,但是如果你想在动作链接中有更多的图像呢? 这是我如何做到的:

  @using (Html.BeginForm("Action", "Controler", ajaxOptions)) { <button type="submit"> <img src="image.png" /> </button> } 

缺点是我仍然需要在button元素上做一些样式,但是你可以把你想要的所有html放在那里。

它也适用于Ajax帮助器: https : //stackoverflow.com/a/19302438/961139