转:Asp.net的URL编码和解码

要解决的问题:
将下面的URL作为一个参数传到其他的页面
1 http://domain/de.apx?uid=12&page=15
2 url后面的参数中出现汉字等,如: ....aspx?title=起重机
在出现上面的情况的时候,必须经过一个URL编码和解码的过程,否则会出现错误.

Server.UrlEncode()则以默认的编码对URL进行编码和解码

 

 string temp = " <a href='Add.aspx?url=" +Server.UrlEncode( skin.Page.Request.Url.AbsoluteUri )+ "&title=" +Server.UrlEncode( skin.Page.Header.Title )+ "'>添加到收藏夹</a>");
 //在另外一个文件中取从上面传的值  解码
 if (Request.QueryString["url"] != null)
    {
     string url = Server.UrlDecode(Request.QueryString["url"].ToString());
    this.txtAddress.Text = url;
 }
if (Request.QueryString["title"] != null)
{
   string title = Server.UrlDecode(Request.QueryString["title"].ToString());
   this.txtTitle.Text = title;
}

 

HttpUtility.UrlEncode()默认是以UTF8对URL进行编码

string tmp1 = System.Web.HttpUtility.UrlEncode(".net技术", System.Text.Encoding.GetEncoding("GB2312"));
string tmp2 = System.Web.HttpUtility.UrlEncode(".net技术", System.Text.Encoding.UTF8);

 

 

 

posted on 2012-05-02 13:57  lei0515  阅读(185)  评论(0)    收藏  举报

导航