程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> 巧用escape解決ASP.NET中URL傳參亂碼問題

巧用escape解決ASP.NET中URL傳參亂碼問題

編輯:關於.NET
程序代碼 <a href="#" onclick="window.open('http://www.webjx.com/test.ASPx?title=網頁教學網');">Links</a>
在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.webjx.com/test.ASPx?title=網頁教學網');">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.webjx.com/test.ASPx?title=網頁教學網');">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.webjx.com/test.ASPx?title=網頁教學網');">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.webjx.com/test.ASPx?title=網頁教學網',300,400);">Links</a>
</body>
</Html>
現在完全不用考慮鏈接所在頁面的編碼方式,也不用絞盡腦汁去想如何在test.ASPx對不同編碼的參數值進行轉換,只需用一個escape就夠了,very good!
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved