开发者

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

I want to display images from other sever开发者_Go百科 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


To display image in a view you could use the <img> tag:

<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..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜