巧用escape解决ASP.NET中URL传参乱码问题
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
{
3
String Request1;
4
Request1 = Request["title"];
5
Response.Write(Request1);
6
}

2

3

4

5

6

有没办法让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
<script language="javascript">
6
function winopen(url,width,height)
7
{
8
var newurl,arrurl;
9
if(typeof(url) == "#ff0000" || url == "")
10
{
11
return ;
12
}
13
else
14
{
15
if(url.indexOf("?") == -1)
16
{
17
newurl = url;
18
}
19
else
20
{
21
newurl = url.substring(0,url.indexOf("?")+1);
22
arrurl = url.substring(url.indexOf("?")+1).split("&");
23
for(var i =0;i<arrurl.length;i++)
24
{
25
newurl += arrurl[i].split("=")[0] + "=" + escape(arrurl[i].split("=")[1]) + "&";
26
}
27
newurl = newurl.substring(0,newurl.length-1);
28
}
29
}
30
if(typeof(width) != "number" || typeof(height) != "number")
31
{
32
window.open(newurl);
33
}
34
else
35
{
36
window.open(newurl,"","width=" + width + ",height=" + height);
37
}
38
}
39
</script>
40
</head>
41
42
<body>
43
<a href="#" onclick="winopen('http://www.mzwu.com/test.aspx?title=木子屋');">Links</a>
44
</body>
45
</html>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

utf8.htm:
1
<html>
2
<head>
3
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4
<title>utf-8测试页</title>
5
<script language="javascript">
6
function winopen(url,width,height)
7
{
8
var newurl,arrurl;
9
if(typeof(url) == "undefined" || url == "")
10
{
11
return ;
12
}
13
else
14
{
15
if(url.indexOf("?") == -1)
16
{
17
newurl = url;
18
}
19
else
20
{
21
newurl = url.substring(0,url.indexOf("?")+1);
22
arrurl = url.substring(url.indexOf("?")+1).split("&");
23
for(var i =0;i<arrurl.length;i++)
24
{
25
newurl += arrurl[i].split("=")[0] + "=" + escape(arrurl[i].split("=")[1]) + "&";
26
}
27
newurl = newurl.substring(0,newurl.length-1);
28
}
29
}
30
if(typeof(width) != "number" || typeof(height) != "number")
31
{
32
window.open(newurl);
33
}
34
else
35
{
36
window.open(newurl,"","width=" + width + ",height=" + height);
37
}
38
}
39
</script>
40
</head>
41
42
<body>
43
<a href="#" onclick="winopen('http://www.mzwu.com/test.aspx?title=木子屋',300,400);">Links</a>
44
</body>
45
</html>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

现在完全不用考虑链接所在页面的编码方式,也不用绞尽脑汁去想如何在test.aspx对不同编码的参数值进行转换,只需用一个escape就够了,very good!
------------------------------------------------
dnawo:天才在于勤奋,知识在于积累!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架