asp.net-mvc ASP .NET MVC RC 中的 Html.Image 在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/509352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:19:01  来源:igfitidea点击:

Where is Html.Image in ASP .NET MVC RC?

asp.net-mvc

提问by Dariusz

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.

我在新的 MVC RC 版本中找不到 Html.Image 方法。请有人给我举例说明如何在 ASP .NET MVC RC 站点中呈现简单的图像。

回答by tvanfosson

Need to write your own or use the Url.Content() method to set the source attribute. Or you can use mine. :-)

需要自己写或者使用Url.Content()方法设置source属性。或者你可以用我的。:-)

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 );
    }
}

Usage:

用法:

<%= Html.Image( Url.Content( "~/Content/images/img.png" ),
               "alt text",
               new { id = "myImage", border = "0" } )
 %>

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.

编辑:自从我写这篇文章后,我就不再使用它了。我会把答案留在这里,但为了后代。虽然我认为它或 Future 程序集中的方法是一个合理的选择,但我发现自己只是使用带有 UrlHelper 提供的 url 的图像标记。当我将我的助手重构为一个独立的库时,这个库没有成功。我会重新评估它是否进入(非未来)MVC 库。

<img src="<%= Url.Content( "~/content/..." ) %>"
     alt="alt text"
     class="has-border" />

回答by Kevin Pang

What's wrong with pure HTML?

纯 HTML 有什么问题?

<img src="" alt="" />

You could use the ViewData collection or model to populate the src attribute:

您可以使用 ViewData 集合或模型来填充 src 属性:

<img src="<%= ViewData["ImageSource"] %>" alt="" />

<img src="<%= ViewData.Model.ImageSource %>" alt="" />

Edit:

编辑:

Regarding using relative urls (i.e. using the "~" character) in the path, change the above code to use ResolveUrl. For example:

关于在路径中使用相对url(即使用“~”字符),将上面的代码改为使用ResolveUrl。例如:

<img src="<%= ResolveUrl(ViewData.Model.ImageSource) %>" alt="" />

回答by Mangesh Pimpalkar

Now we can return MvcHtmlString.

现在我们可以返回 MvcHtmlString。

public static MvcHtmlString 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 MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
        }

usage in razor:

在剃须刀中的用法:

@Html.Image( Url.Content( "~/Content/images/img.png" ),
               "alt text",
               new { id = "myImage", border = "0" } )

回答by Md Jahangir Alam

public static class HtmlImageHelper
{
    public static IHtmlString Image(this HtmlHelper helper,
                            string src,
                            string alt)
    {
        TagBuilder builder = new TagBuilder("img");
        builder.Attributes.Add("src", VirtualPathUtility.ToAbsolute(src));
        builder.Attributes.Add("alt", alt);
        return new HtmlString(builder.ToString(TagRenderMode.SelfClosing));
    }
}

@Html.Image(@Model.Image, @Model.alt)

@Html.Image(@Model.Image, @Model.alt)

回答by Simon_Weaver

Its in MVC futures - which they are updating with each release.

它在 MVC 期货中 - 他们随着每个版本的更新而更新。

RC2 futures is here : http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24142

RC2 期货在这里:http: //aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId= 24142

Don't feel guilty about using something in 'futures'. Its not going anywhere - and even if it ever did you have access to the source.

不要为在“未来”中使用某些东西而感到内疚。它不会去任何地方 - 即使它曾经让您访问过源代码。

You need to be slightly cautious about some things in futures (in case they disappear) but you can be pretty sure Image helper isnt going anywhere.

您需要对期货中的某些事情保持谨慎(以防它们消失),但您可以确定 Image helper 不会去任何地方。

回答by Tomas Aschan

If you need to reference an image in, say, the ~/Content/images/folder, it can easily be done with just plain HTML:

如果您需要在~/Content/images/文件夹中引用图像,只需使用纯 HTML 即可轻松完成:

<img src="/Content/image/mypic.png" alt="a picture available anywhere" />

The trick is the "/" at the beginning of the srcattribute, which will tell the browser to start browsing folders at the root of the site. As ASP.NET MVC by default does not parse route urls on files that do exist on the server, the file will be retrieved as usual. This will also work on the Indexview of the Homecontroller regardless of whether you have the url http://www.example.com/, http://www.example.com/Homeor http://www.example.com/Home/Index

诀窍是属性/开头的“ ” src,它会告诉浏览器开始浏览站点根目录下的文件夹。由于默认情况下 ASP.NET MVC 不会解析服务器上确实存在的文件的路由 url,因此将照常检索该文件。无论您是否拥有 url ,这也适用于控制器的Index视图,或者Homehttp://www.example.com/http://www.example.com/Homehttp://www.example.com/Home/Index