简体   繁体   中英

how to display image using view and controller by ASP.NET MVC

I want to display images from other sever by using view and controller by asp.net mvc. how can i do? can u tell me detail and give me detail an exmaple? wait to see your answer.

Thanks Nara

要在视图中显示图像,可以使用<img>标签:

<img src="http://someotherserver/path/to/some/image.png" alt="" />

or you could make a little html helper:

public static MvcHtmlString Image(this HtmlHelper helper,
                            string url,
                            object htmlAttributes)
{
    return Image(helper, url, null, htmlAttributes);
}
public static MvcHtmlString Image(this HtmlHelper helper,
                                string url,
                                string altText,
                                object htmlAttributes)
{
    TagBuilder builder = new TagBuilder("image");

    var path = url.Split('?');
    string pathExtra = "";
    if(path.Length >1)
    {
        pathExtra = "?" + path[1];
    }
    builder.Attributes.Add("src", VirtualPathUtility.ToAbsolute(path[0]) + pathExtra);
    builder.Attributes.Add("alt", altText);
    builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
    return MvcHtmlString.Create( builder.ToString(TagRenderMode.SelfClosing));
}

typical usage:

<%=Html.Image("~/content/images/ajax-loader.gif", new{style="margin: 0 auto;"})%>

enjoy..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM