哪里是在Html.Image .NET ASP MVC RC? [英] Where is Html.Image in ASP .NET MVC RC?
问题描述
我无法找到新的MVC RC版Html.Image方法。请别人给我的例子如何使简单的图像在ASP .NET MVC RC站点。
I can't find the Html.Image method in new MVC RC version. Please somebody give me example how to render simple Image in ASP .NET MVC RC sites.
推荐答案
需要编写自己的或使用Url.Content()方法来设置源属性。或者你可以使用我的。 : - )
Need to write your own or use the Url.Content() method to set the source attribute. Or you can use mine. :-)
public static class HtmlHelperExtensions
{
public static string Image( this HtmlHelper helper,
string url,
string altText,
object htmlAttributes )
{
TagBuilder builder = new TagBuilder( "img" );
builder.Attributes.Add( "src", url );
builder.Attributes.Add( "alt", altText );
builder.MergeAttributes( new RouteValueDictionary( htmlAttributes ) );
return builder.ToString( TagRenderMode.SelfClosing );
}
}
用法:
<%= Html.Image( Url.Content( "~/Content/images/img.png" ),
"alt text",
new { id = "myImage", border = "0" } )
%>
修改:自从我写了这个,我使用它消失。我将离开这里的答案,但为后人。虽然我觉得,还是从未来的装配方法,是一种合理的选择,我发现自己仅使用由一个UrlHelper提供的URL图像标记。当我重构我的助手到一个独立的库,这其中并没有获得晋级。我会重新评估,如果它使成(非期货)MVC库。
EDIT: Since I wrote this I've gone away from using it. I'll leave the answer here, though for posterity. While I think it, or the method from the Future assembly, is a reasonable choice, I find myself simply using the image tag with the url supplied by a UrlHelper. When I refactored my helpers into a standalone library, this one didn't make the cut. I'll re-evaluate if it makes it into the (non-futures) MVC library.
<img src="<%= Url.Content( "~/content/..." ) %>"
alt="alt text"
class="has-border" />
这篇关于哪里是在Html.Image .NET ASP MVC RC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!