C# HTMLENCODE图像URL并断开图像
我有一个简单的脚本,可以保存新闻详细信息,如C# HTMLENCODE图像URL并断开图像,c#,html,asp.net,webforms,C#,Html,Asp.net,Webforms,我有一个简单的脚本,可以保存新闻详细信息,如新闻标题、新闻URL和新闻图像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.
新闻标题、新闻URL和新闻图像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#URL编码/解码:
string encodedUrl = HttpUtility.UrlEncode(myUrl);
string sameMyUrl = HttpUtility.UrlDecode(encodedUrl);
function myFunction() {
var uri = myUrl;
var uri_enc = encodeURIComponent(uri);
var uri_dec = decodeURIComponent(uri_enc);
}
string encodedHtml = HttpUtility.HtmlEncode(myHtml);
string sameMyHtml = HttpUtility.HtmlDecode(encodedHtml);
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();
}
Javascript URL编码/解码:
string encodedUrl = HttpUtility.UrlEncode(myUrl);
string sameMyUrl = HttpUtility.UrlDecode(encodedUrl);
function myFunction() {
var uri = myUrl;
var uri_enc = encodeURIComponent(uri);
var uri_dec = decodeURIComponent(uri_enc);
}
string encodedHtml = HttpUtility.HtmlEncode(myHtml);
string sameMyHtml = HttpUtility.HtmlDecode(encodedHtml);
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();
}
C#HTML编码/解码:
string encodedUrl = HttpUtility.UrlEncode(myUrl);
string sameMyUrl = HttpUtility.UrlDecode(encodedUrl);
function myFunction() {
var uri = myUrl;
var uri_enc = encodeURIComponent(uri);
var uri_dec = decodeURIComponent(uri_enc);
}
string encodedHtml = HttpUtility.HtmlEncode(myHtml);
string sameMyHtml = HttpUtility.HtmlDecode(encodedHtml);
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();
}
Javascript HTML编码/解码:
string encodedUrl = HttpUtility.UrlEncode(myUrl);
string sameMyUrl = HttpUtility.UrlDecode(encodedUrl);
function myFunction() {
var uri = myUrl;
var uri_enc = encodeURIComponent(uri);
var uri_dec = decodeURIComponent(uri_enc);
}
string encodedHtml = HttpUtility.HtmlEncode(myHtml);
string sameMyHtml = HttpUtility.HtmlDecode(encodedHtml);
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();
}
函数htmlEncode(值){
//创建内存中的div,设置其内部文本(jQuery自动编码)
//然后把编码的内容拿回来。div在页面上永远不存在。
返回$('').text(value.html();
}
函数htmlDecode(html){
返回$('').html(html.text();
}