1

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

Toeur Tenh
  • 71
  • 4
  • 14

2 Answers2

2

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

<img src="http://someotherserver/path/to/some/image.png" alt="" />
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

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

jim tollan
  • 22,305
  • 4
  • 49
  • 63