用户名: 密码: 验证码: 注册           网站地图 高级搜索 RSS订阅 收藏本站
山东十七地市站长联盟信息: 济南 青岛 淄博 枣庄 东营 烟台 潍坊 济宁 泰安 威海 日照 莱芜 临沂 德州 聊城 滨州 菏泽      
您的位置:主页>网络编程>.Net编程>

解决ASP.NET中URL传参乱码问题

[ 来源: | 作者: | 更新日期:2007-12-13 11:22:21 | 评论 0 条 | 我要投稿 ]

程序代码
<a href="#" onclick="window.open('http://www.webjx.com/test.aspx?title=网页教学网');">Links</a>

字串5

在test.aspx中,只要获取title参数的值并显示出来即可,本来用Request["title"]就可解决的问题却因链接所处页面的编码不同而变得复杂起来:

字串6

当链接所处的页面是用GB2312编码时,如果test.aspx也是GB2312则获取的参数值不乱码,否则乱码;
当链接所处的页面是用UTF-8编码时,如果test.aspx也是UTF-8则获取的参数值不乱码,否则乱码;

字串6

gb.htm: 字串9

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>gb2312测试页</title>
</head>

字串2

<body>
<a href="#" onclick="window.open('http://www.webjx.com/test.aspx?title=网页教学网');">Links</a>
</body>
</html>

字串4

utf8.htm: 字串8

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 字串8

<title>utf-8测试页</title>
</head>

字串8

<body>
<a href="#" onclick="window.open('http://www.webjx.com/test.aspx?title=网页教学网');">Links</a>
</body>
</html>

字串5

test.aspx.cs: 字串2

private void Page_Load(object sender, System.EventArgs e)
{
    String Request1;
    Request1 = Request["title"];
    Response.Write(Request1);
} 字串1

有没办法让test.aspx不论URL中的参数以何种方式编码都能正常的获取显示呢?通过配置web.config的<globalization requestEncoding="gb2312|utf-8" />都只会顾此失彼,不能完美的解决我们的问题。最终,在老农的提示下使用JS的escape问题得以完美解决:
gb.htm: 字串9

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>gb2312测试页</title> 字串2


<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("&"); 字串1
            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);

字串1

    }
}
</script>
</head>

字串7

<body>
<a href="#" onclick="winopen('http://www.webjx.com/test.aspx?title=网页教学网');">Links</a>
</body>
</html>

字串2

utf8.htm: 字串4

<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);

字串6


            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);
    }
字串2

    else
    {
        window.open(newurl,"","width=" + width + ",height=" + height);
    }
}
</script>
</head> 字串5

<body>
<a href="#" onclick="winopen('http://www.webjx.com/test.aspx?title=网页教学网',300,400);">Links</a>
</body>
</html> 字串4

现在完全不用考虑链接所在页面的编码方式,也不用绞尽脑汁去想如何在test.aspx对不同编码的参数值进行转换,只需用一个escape就够了, 字串8


Tags:问题 解决 newurl url width height title arrurl window.open
您的评论
用户名:新注册) 密码: 匿名评论 [所有评论]

·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为