ASP.NET中URL传参数出现乱码解决方法
2008-06-07 15:02
696 查看
在test.aspx中,只要获取title参数的值并显示出来即可,本来用Request["title"]就可解决的问题却因链接所处页面的编码不同而变得复杂起来:
当链接所处的页面是用GB2312编码时,如果test.aspx也是GB2312则获取的参数值不乱码,否则乱码;
当链接所处的页面是用UTF-8编码时,如果test.aspx也是UTF-8则获取的参数值不乱码,否则乱码;
gb.htm:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>gb2312测试页</title>
</head>
<body>
<a href="#" onclick="window.open('http://www.aaa.com');">Links</a>
</body>
</html>
utf8.htm:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>utf-8测试页</title>
</head>
<body>
<a href="#" onclick="window.open('http://www.aaa.com');">Links</a>
</body>
</html>
test.aspx.cs:
private void Page_Load(object sender, System.EventArgs e)
{
String Request1;
Request1 = Request["title"];
Response.Write(Request1);
}
有没办法让test.aspx不论URL中的参数以何种方式编码都能正常的获取显示呢?通过配置web.config的< globalization requestEncoding="gb2312|utf-8" />都只会顾此失彼,不能完美的解决我们的问题。最终,在老农的提示下使用JS的escape问题得以完美解决:
gb.htm:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>gb2312测试页</title>
<script language="javascript">
function winopen(url,width,height)
{
var newurl,arrurl;
if(typeof(url) == "undefined" || url == "")
{
return ;
}
else
{
if(url.indexOf("?") == -1)
{
newurl = url;
}
else
{
newurl = url.substring(0,url.indexOf("?")+1);
arrurl = url.substring(url.indexOf("?")+1).split("&");
for(var i =0;i<arrurl.length;i++)
{
newurl += arrurl[i].split("=")[0] + "=" + escape(arrurl[i].split("=")[1]) + "&";
}
newurl = newurl.substring(0,newurl.length-1);
}
}
if(typeof(width) != "number" || typeof(height) != "number")
{
window.open(newurl);
}
else
{
window.open(newurl,"","width=" + width + ",height=" + height);
}
}
</script>
</head>
<body>
<a href="#" onclick="winopen('http://www.aaa.com">Links</a>
</body>
</html>
utf8.htm:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>utf-8测试页</title>
<script language="javascript">
function winopen(url,width,height)
{
var newurl,arrurl;
if(typeof(url) == "undefined" || url == "")
{
return ;
}
else
{
if(url.indexOf("?") == -1)
{
newurl = url;
}
else
{
newurl = url.substring(0,url.indexOf("?")+1);
arrurl = url.substring(url.indexOf("?")+1).split("&");
for(var i =0;i<arrurl.length;i++)
{
newurl += arrurl[i].split("=")[0] + "=" + escape(arrurl[i].split("=")[1]) + "&";
}
newurl = newurl.substring(0,newurl.length-1);
}
}
if(typeof(width) != "number" || typeof(height) != "number")
{
window.open(newurl);
}
else
{
window.open(newurl,"","width=" + width + ",height=" + height);
}
}
</script>
</head>
<body>
<a href="#" onclick="winopen('http://www.aaa.com');">Links</a>
</body>
</html>
现在完全不用考虑链接所在页面的编码方式,也不用绞尽脑汁去想如何在test.aspx对不同编码的参数值进行转换,只需用一个escape就够了,very good!
当链接所处的页面是用GB2312编码时,如果test.aspx也是GB2312则获取的参数值不乱码,否则乱码;
当链接所处的页面是用UTF-8编码时,如果test.aspx也是UTF-8则获取的参数值不乱码,否则乱码;
gb.htm:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>gb2312测试页</title>
</head>
<body>
<a href="#" onclick="window.open('http://www.aaa.com');">Links</a>
</body>
</html>
utf8.htm:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>utf-8测试页</title>
</head>
<body>
<a href="#" onclick="window.open('http://www.aaa.com');">Links</a>
</body>
</html>
test.aspx.cs:
private void Page_Load(object sender, System.EventArgs e)
{
String Request1;
Request1 = Request["title"];
Response.Write(Request1);
}
有没办法让test.aspx不论URL中的参数以何种方式编码都能正常的获取显示呢?通过配置web.config的< globalization requestEncoding="gb2312|utf-8" />都只会顾此失彼,不能完美的解决我们的问题。最终,在老农的提示下使用JS的escape问题得以完美解决:
gb.htm:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>gb2312测试页</title>
<script language="javascript">
function winopen(url,width,height)
{
var newurl,arrurl;
if(typeof(url) == "undefined" || url == "")
{
return ;
}
else
{
if(url.indexOf("?") == -1)
{
newurl = url;
}
else
{
newurl = url.substring(0,url.indexOf("?")+1);
arrurl = url.substring(url.indexOf("?")+1).split("&");
for(var i =0;i<arrurl.length;i++)
{
newurl += arrurl[i].split("=")[0] + "=" + escape(arrurl[i].split("=")[1]) + "&";
}
newurl = newurl.substring(0,newurl.length-1);
}
}
if(typeof(width) != "number" || typeof(height) != "number")
{
window.open(newurl);
}
else
{
window.open(newurl,"","width=" + width + ",height=" + height);
}
}
</script>
</head>
<body>
<a href="#" onclick="winopen('http://www.aaa.com">Links</a>
</body>
</html>
utf8.htm:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>utf-8测试页</title>
<script language="javascript">
function winopen(url,width,height)
{
var newurl,arrurl;
if(typeof(url) == "undefined" || url == "")
{
return ;
}
else
{
if(url.indexOf("?") == -1)
{
newurl = url;
}
else
{
newurl = url.substring(0,url.indexOf("?")+1);
arrurl = url.substring(url.indexOf("?")+1).split("&");
for(var i =0;i<arrurl.length;i++)
{
newurl += arrurl[i].split("=")[0] + "=" + escape(arrurl[i].split("=")[1]) + "&";
}
newurl = newurl.substring(0,newurl.length-1);
}
}
if(typeof(width) != "number" || typeof(height) != "number")
{
window.open(newurl);
}
else
{
window.open(newurl,"","width=" + width + ",height=" + height);
}
}
</script>
</head>
<body>
<a href="#" onclick="winopen('http://www.aaa.com');">Links</a>
</body>
</html>
现在完全不用考虑链接所在页面的编码方式,也不用绞尽脑汁去想如何在test.aspx对不同编码的参数值进行转换,只需用一个escape就够了,very good!
相关文章推荐
- ASP.NET中URL传参数出现乱码解决方法
- 在ASP.NET里通过URL传递参数得到乱码的解决方法
- [转]asp.net URL中包含中文参数造成乱码的解决方法
- Asp.Net Url 传值出现乱码的解决方法
- Asp.Net Url传值出现乱码的解决方法。
- Asp.Net Url 传值出现乱码的解决方法(包括js传值)
- Asp.Net Url 传值出现乱码的解决方法
- Asp.Net Url 传值出现乱码的解决方法
- Asp.Net Url 传值出现乱码的解决方法(包括js传值)
- asp.net URL中包含中文参数造成乱码的解决方法
- URL编码:在地栏直接输入参数带汉字的URL后,服务器端接收后出现乱码的解决方法
- asp.net页面通过URL参数传值中文乱码问题解决办法
- ASP.NET Core2.0 使用VSCode 版本1.13.1工具 开发控制台运行出现中文乱码问题及解决方法
- asp.net传中文值出现乱码的一个解决方法
- C# asp.net页面通过URL参数传值中文乱码问题解决办法
- url传参数出现乱码解决方法
- Asp.net 2.0 Request ,Response 出现乱码 Email乱码 解决方法
- ASP.NET中url传递中文的解决方案,传递参数为汉字时出现乱码等问题
- JSP页面通过URL传递中文参数出现中文乱码的解决方法