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

所属分类: 网络编程 / ASP.NET 阅读数: 1699
收藏 0 赞 0 分享
在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));

要点是:
将传递的中文参数进行编码,在接收时再进行解码。
完。
更多精彩内容其他人还在看

Asp.net中的mail的发送

Asp.net中的mail的发送
收藏 0 赞 0 分享

用ASP.Net实现文件的在线压缩和解压缩

用ASP.Net实现文件的在线压缩和解压缩
收藏 0 赞 0 分享

ASP.NET中文件上传下载方法集合

ASP.NET中文件上传下载方法集合
收藏 0 赞 0 分享

ASP.NET通过Remoting service上传文件

ASP.NET通过Remoting service上传文件
收藏 0 赞 0 分享

ASP.NET2.0服务器控件之Render方法

ASP.NET2.0服务器控件之Render方法
收藏 0 赞 0 分享

ASP.NET2.0 WebRource,开发微调按钮控件

ASP.NET2.0 WebRource,开发微调按钮控件
收藏 0 赞 0 分享

ASP.NET2.0新特性概述

ASP.NET2.0新特性概述
收藏 0 赞 0 分享

介绍几个ASP.NET中容易忽略但却很重要的方法函数

介绍几个ASP.NET中容易忽略但却很重要的方法函数
收藏 0 赞 0 分享

asp.net2.0如何加密数据库联接字符串

asp.net2.0如何加密数据库联接字符串
收藏 0 赞 0 分享

用.NET 2.0压缩/解压功能处理大型数据

用.NET 2.0压缩/解压功能处理大型数据
收藏 0 赞 0 分享
查看更多