当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net中url地址传送中文参数时的两种解决方案

asp.net中url地址传送中文参数时的两种解决方案

2017年12月12日  | 移动技术网IT编程  | 我要评论

江苏智能家居,广东三b院校,杉原杏璃 qvod

在web.comfig中配置 是一样的:
<globalization requestencoding="gb2312" responseencoding="gb2312"/>
页面header部分也都有
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
真是奇怪,
只好用了笨办法:
写参数:
复制代码 代码如下:

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


读参数:
复制代码 代码如下:

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,与前面对应

后来,看了孟子的文章,才发现有更好的解决方案:
用javascript!
写一个方法放在基类页面中
复制代码 代码如下:

public void pagelocation(string chineseurl)
{
if(chineseurl==null || chineseurl.trim().length==0 )
{return;//还可能不是一个合法的url tony 2007/11/15
}
page.clientscript.registerstartupscript(this.gettype(), "agronetpagelocationto", "<script type='text/javascript' language='javascript'> window.location.href='"+chineseurl+"';</script>");
}

然后在页面中调用
复制代码 代码如下:

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,实际应用在分页时需要保持中文参数,最好还是用window.location.href方法!

最后,如果一要在javascript与.net后台代码进行对话,可以这样:
复制代码 代码如下:

<script language= "javascript " >
function gourl()
{
var name = "中文参数 ";
location.href = "b.aspx?name= "+escape(name);
}
</script >
<body onclick= "gourl() " >

接收:
复制代码 代码如下:

string name = request.querystring[ "name "];
response.write(httputility.urldecode(name));

要点是:
将传递的中文参数进行编码,在接收时再进行解码。
完。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网