asp.net中url地址傳送中文參數(shù)時(shí)的兩種解決方案
更新時(shí)間:2009年11月09日 18:24:54 作者:
前天遇到一個(gè)地址傳遞中文參數(shù)變?yōu)閬y碼的問題,同樣的兩個(gè)web Project,一個(gè)是vs2003,一個(gè)是vs2005,前者可以,后者就是不可以。
在Web.comfig中配置 是一樣的:
<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>
頁面Header部分也都有
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
真是奇怪,
只好用了笨辦法:
寫參數(shù):
string strurl = PreUrl + "?word={0}&sort={1}&check={2}";
strurl = string.Format(strurl, HttpUtility.UrlEncode(this.txtSearchTxt.Text.Trim(), System.Text.Encoding.GetEncoding("GB2312")), this.radioSortDesc.SelectedIndex.ToString(), CheckState.ToString());
Page.Response.Redirect(strurl);
//注意編碼方式為gb2312
讀參數(shù):
try
{ if (Page.Request.QueryString["word"] != null)
{ _word = Convert.ToString(HttpUtility.UrlDecode(Page.Request.QueryString["word"], System.Text.Encoding.GetEncoding("GB2312"))); }
}
catch { _word = String.Empty; }
///注意編碼方式為gb2312,與前面對應(yīng)
后來,看了孟子的文章,才發(fā)現(xiàn)有更好的解決方案:
用Javascript!
寫一個(gè)方法放在基類頁面中
public void PageLocation(string chineseURL)
{
if(chineseURL==null || chineseURL.Trim().Length==0 )
{return;//還可能不是一個(gè)合法的URL Tony 2007/11/15
}
Page.ClientScript.RegisterStartupScript(this.GetType(), "AgronetPageLocationTo", "<script type='text/javascript' language='javascript'> window.location.href='"+chineseURL+"';</script>");
}
然后在頁面中調(diào)用
string strurl = PreUrl + "?word={0}&sort={1}&check={2}";
strurl = string.Format(strurl, this.txtSearchTxt.Text.Trim(), this.radioSortDesc.SelectedIndex.ToString(), CheckState.ToString());
PageLocation(strurl);
注意后種方法用了Javasrcipt,實(shí)際應(yīng)用在分頁時(shí)需要保持中文參數(shù),最好還是用window.Location.Href方法!
最后,如果一要在javascript與.net后臺代碼進(jìn)行對話,可以這樣:
<script language= "JavaScript " >
function GoUrl()
{
var Name = "中文參數(shù) ";
location.href = "B.aspx?Name= "+escape(Name);
}
</script >
<body onclick= "GoUrl() " >
接收:
string Name = Request.QueryString[ "Name "];
Response.Write(HttpUtility.UrlDecode(Name));
要點(diǎn)是:
將傳遞的中文參數(shù)進(jìn)行編碼,在接收時(shí)再進(jìn)行解碼。
完。
<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>
頁面Header部分也都有
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
真是奇怪,
只好用了笨辦法:
寫參數(shù):
復(fù)制代碼 代碼如下:
string strurl = PreUrl + "?word={0}&sort={1}&check={2}";
strurl = string.Format(strurl, HttpUtility.UrlEncode(this.txtSearchTxt.Text.Trim(), System.Text.Encoding.GetEncoding("GB2312")), this.radioSortDesc.SelectedIndex.ToString(), CheckState.ToString());
Page.Response.Redirect(strurl);
//注意編碼方式為gb2312
讀參數(shù):
復(fù)制代碼 代碼如下:
try
{ if (Page.Request.QueryString["word"] != null)
{ _word = Convert.ToString(HttpUtility.UrlDecode(Page.Request.QueryString["word"], System.Text.Encoding.GetEncoding("GB2312"))); }
}
catch { _word = String.Empty; }
///注意編碼方式為gb2312,與前面對應(yīng)
后來,看了孟子的文章,才發(fā)現(xiàn)有更好的解決方案:
用Javascript!
寫一個(gè)方法放在基類頁面中
復(fù)制代碼 代碼如下:
public void PageLocation(string chineseURL)
{
if(chineseURL==null || chineseURL.Trim().Length==0 )
{return;//還可能不是一個(gè)合法的URL Tony 2007/11/15
}
Page.ClientScript.RegisterStartupScript(this.GetType(), "AgronetPageLocationTo", "<script type='text/javascript' language='javascript'> window.location.href='"+chineseURL+"';</script>");
}
然后在頁面中調(diào)用
復(fù)制代碼 代碼如下:
string strurl = PreUrl + "?word={0}&sort={1}&check={2}";
strurl = string.Format(strurl, this.txtSearchTxt.Text.Trim(), this.radioSortDesc.SelectedIndex.ToString(), CheckState.ToString());
PageLocation(strurl);
注意后種方法用了Javasrcipt,實(shí)際應(yīng)用在分頁時(shí)需要保持中文參數(shù),最好還是用window.Location.Href方法!
最后,如果一要在javascript與.net后臺代碼進(jìn)行對話,可以這樣:
復(fù)制代碼 代碼如下:
<script language= "JavaScript " >
function GoUrl()
{
var Name = "中文參數(shù) ";
location.href = "B.aspx?Name= "+escape(Name);
}
</script >
<body onclick= "GoUrl() " >
接收:
復(fù)制代碼 代碼如下:
string Name = Request.QueryString[ "Name "];
Response.Write(HttpUtility.UrlDecode(Name));
要點(diǎn)是:
將傳遞的中文參數(shù)進(jìn)行編碼,在接收時(shí)再進(jìn)行解碼。
完。
相關(guān)文章
ASP.Net Core基于ABP架構(gòu)配置To Json序列化
這篇文章介紹了ASP.Net Core基于ABP架構(gòu)配置To Json序列化的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
大型門戶網(wǎng)站實(shí)現(xiàn)的十四大技術(shù)小結(jié)
參考下大型門戶網(wǎng)站的技術(shù),大家可以盡量的備份好服務(wù)器。2010-10-10
c# 連接字符串?dāng)?shù)據(jù)庫服務(wù)器端口號 .net狀態(tài)服務(wù)器端口號
正常的數(shù)據(jù)庫連接字符串配置,這是在MSSQL服務(wù)器端口是1433(默認(rèn))的情況下。2009-06-06
asp.net String.Empty NULL 不同之處
在asp.net(c#)中String.Empty、NULL、"" 3個(gè)語法經(jīng)常使用,作用是判斷字符串是否為空。2009-06-06
ASP.NET生成兩個(gè)日期范圍內(nèi)隨機(jī)時(shí)間的實(shí)現(xiàn)方法
這篇文章主要介紹了ASP.NET生成兩個(gè)日期范圍內(nèi)隨機(jī)時(shí)間的實(shí)現(xiàn)方法,通過自定義函數(shù)記錄開始時(shí)間與結(jié)束時(shí)間確定時(shí)間范圍進(jìn)而生成該時(shí)間段的隨機(jī)時(shí)間,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12

