【问题标题】:HTMLEncodes Image URL and breaks the imageHTMLEncodes 图片 URL 并破坏图片
【发布时间】:2015-05-06 08:14:22
【问题描述】:

我有一个简单的脚本,我可以在其中保存 News Title, News URL and News Image URL 之类的新闻详细信息。我注意到图像在包含 Unicode 字符时不显示,例如 http://www.bbj.hu/images2/201412/párizsi_ud_20141218113410452.jpg

它按原样存储在数据库中,但是当我在网页中断时显示它并显示为 http://www.bbj.hu/images2/201412/pa%c2%b4rizsi_ud_20141218113410452.jpg

当我调试我的 asp.net webform 页面时,它在后面的代码中正确显示

 protected String getImage(object imgSource)
    {
        string img = null;
        img = imgSource.ToString();
        return img; 

// 调试正确显示图像 url,但它在实际页面上中断 }

.aspx 代码

  <asp:Image ID="NewsImage" ImageUrl='<%# getImage(Eval("NewsImageURL")) %>'  runat="server"  />

我尝试了不同的方法,但它一直显示为http://www.bbj.hu/images2/201412/pa%c2%b4rizsi_ud_20141218113410452.jpg

我该如何解决这个问题

【问题讨论】:

    标签: c# html asp.net webforms


    【解决方案1】:

    您的问题的解决方案必须属于以下一项或多项:

    C# URL 编码/解码:

    string encodedUrl = HttpUtility.UrlEncode(myUrl);
    string sameMyUrl = HttpUtility.UrlDecode(encodedUrl);
    

    Javascript URL 编码/解码:

    function myFunction() {
        var uri = myUrl;
        var uri_enc = encodeURIComponent(uri);
        var uri_dec = decodeURIComponent(uri_enc);
    }
    

    C# HTML 编码/解码:

    string encodedHtml = HttpUtility.HtmlEncode(myHtml);
    string sameMyHtml = HttpUtility.HtmlDecode(encodedHtml);
    

    Javascript HTML 编码/解码:

    function htmlEncode(value) {
         //create a in-memory div, set its inner text (which jQuery automatically encodes)
         //then grab the encoded contents back out. The div never exists on the page.
         return $('<div/>').text(value).html();
    }
    
    function htmlDecode(html) {
        return $('<div>').html(html).text();
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-27
      • 2015-12-02
      • 2017-06-05
      • 2017-10-10
      • 2018-02-27
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      • 2017-09-19
      相关资源
      最近更新 更多