【问题标题】:how to display image using view and controller by ASP.NET MVC如何通过 ASP.NET MVC 使用视图和控制器显示图像
【发布时间】:2011-05-24 11:14:37
【问题描述】:

我想通过 asp.net mvc 使用视图和控制器来显示来自其他服务器的图像。我能怎么做?你能告诉我细节并给我一个详细的例子吗?等着看你的答案。

谢谢 奈良

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    或者你可以做一个小 html 助手:

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

    典型用法:

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

    享受..

    【讨论】:

      【解决方案2】:

      要在视图中显示图像,您可以使用&lt;img&gt; 标签:

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

      【讨论】:

        猜你喜欢
        • 2014-09-23
        • 1970-01-01
        • 2012-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多