1
<a href="#" onclick="window.open('http://www.mzwu.com/test.aspx?title=木子屋');">Links</a>
在test.aspx中,只要获取title参数的值并显示出来即可,本来用Request["title"]就可解决的问题却因链接所处页面的编码不同而变得复杂起来:
当链接所处的页面是用GB2312编码时,如果test.aspx也是GB2312则获取的参数值不乱码,否则乱码;
当链接所处的页面是用UTF-8编码时,如果test.aspx也是UTF-8则获取的参数值不乱码,否则乱码;
gb.htm:
1
<html>
2
<head>
3
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
4
<title>gb2312测试页</title>
5
</head>
6
7
<body>
8
<a href="#" onclick="window.open('http://www.mzwu.com/test.aspx?title=木子屋');">Links</a>
9
</body>
10
</html>
2
3
4
5
6
7
8
9
10
utf8.htm:
1
<html>
2
<head>
3
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4
<title>utf-8测试页</title>
5
</head>
6
7
<body>
8
<a href="#" onclick="window.open('http://www.mzwu.com/test.aspx?title=木子屋');">Links</a>
9
</body>
10
</html>
2
3
4
5
6
7
8
9
10
test.aspx.cs:
1
private void Page_Load(object sender, System.EventArgs e)
2
}
2
有没办法让test.aspx不论URL中的参数以何种方式编码都能正常的获取显示呢?通过配置web.config的<globalization requestEncoding="gb2312|utf-8" />都只会顾此失彼,不能完美的解决我们的问题。最终,在老农的提示下使用JS的escape问题得以完美解决:
gb.htm:
1
<html>
2
<head>
3
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
4
<title>gb2312测试页</title>
5
>
2
3
4
5
utf8.htm:
1
<html>
2
<head>
3
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4
<title>utf-8测试页</title>
5
>
2
3
4
5
现在完全不用考虑链接所在页面的编码方式,也不用绞尽脑汁去想如何在test.aspx对不同编码的参数值进行转换,只需用一个escape就够了,very good!