Wednesday, February 10, 2010

How to use urlEncode to encode Request.Url?

Microsoft UrlEncode is designed to encode untrusted data within URL context. It is not meant to encode whole URL. However, sometimes, we do need to encode whole URL. Here is some codes that can do it:

String pagingUrl = string.Empty;
//get the url part without query string
pagingUrl = Request.Url.GetLeftPart(UriPartial.Path) + "?";
NameValueCollection coll = Request.QueryString;

// encode the name and values for all query strings
foreach (String key in coll.Keys)
{
pagingUrl += AntiXss.UrlEncode(key) + "=" + AntiXss.UrlEncode(coll[key]) + "&" ;
}



No comments:

Post a Comment