asp.net預設的編碼是UTF-8
js檔案裡的編碼也是UTF-8
當你要在aspx頁面上進行傳中文參數時會出現亂碼
<-----request.aspx--接收參數頁----->
<----response.aspx--傳送參數頁----->
例一:<a href="request.aspx?str=中國人"></a>
解決辦法一:
1.可以和改webconfig的編碼 如:
<location path='response.aspx'>
<system.web>
<globalization fileEncoding='gb2312' requestEncoding='gb2312' responseEncoding='gb2312' culture='zh-CN'/>
</system.web>
</location>
注意:你也要把request.aspx頁面上的編碼也改成同樣的,雖然中文亂碼解決了,但如果你用到了js檔案就會出現亂碼
//用這以上方法的話不會改變網站的其它頁面上的編碼
<location path='request.aspx'>
解決辦法二:
1.如果你不想動webconfig 你可以在”response.aspx.cs“裡面對參數進行編碼 如:
response.aspx在頁面上:
<a href="request.aspx?str=<%=str%>"></a>
response.cs頁面上:
聲明一個變量str
public string str="中國人";
str= HttpUtility.UrlEncode(str,System.Text.Encoding.GetEncoding("GB2312"));
//這時str已經是編碼後的
2.而在request.aspx.cs檔案中也要進行轉換 如:
聲明一個變量 System.Collections.Specialized.NamueCollection gb2312=HttpUtility.ParseQueryString(Request.Url.Query,System.Text.Encoding.GetEncoding("GB2312"));
string str=gb2312["str"];
這裡的str就是你要接收的中文。
例二
如果你想在js裡面傳送中文參數 如:
request.aspx?str="+encodeURI("中國人");
這樣就不會出現亂碼了
例三
就是我在cshn上找到的一個方法我也沒試過,大家可以試一下
protected string GetQueryString(string sKey,System.Text.Encoding e)
{string QueryString=Server.UrlDecode(System.Web.HttpUtility.UrlDecode(Request.ServerVariables[ "QUERY_STRING "],e));
System.Text.Regularexpression_rs.Regex reg = new System.Text.Regularexpression_rs.Regex(sKey+"=([^&$]*?)(&|$) ");
System.Text.Regularexpression_rs.Match m = reg.Match(QueryString);
if (m.Success) { return m.Result( "$1 "); }
else return String.Empty;
}
//以上這個不受編碼影響,隻需知道原來傳入的編碼就可.