Hi Mythran, and thanks for the response.
Your method seems to work ... but I don't think that encoding the entire url
is necessary. It seems wasteful to unnecessarily substitute so many
characters. Which characters really need to be encoded ?
Here are the results of my test:
string categoryId = "100";
string categoryName = "toys & games";
string urlFormat =
"http://www.mywebsite.c om/mypage.aspx?cat egory={0}&filte r=price";
string tagFormat = "<a href=\"{0}\">{1 }</a>";
string url = Server.UrlEncod e(string.Format (urlFormat, categoryId));
string html = string.Format(t agFormat, url, categoryName);
html = <a
href="http%3a%2 f%2fwww.mywebsi te.com%2fmypage .aspx%3fcategor y%3d100%26filte r%3dprice">toys
& games</a>
"Mythran" <ki********@hot mail.comREMOVET RAIL> wrote in message
news:%2******** *******@TK2MSFT NGP02.phx.gbl.. .
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:el******** ******@TK2MSFTN GP05.phx.gbl...I am using a StringBuilder to build a link tag (based on categoryId and
categoryNam e , which are populated elsewhere ) :
StringBuilder sb = new StringBuilder() ;
sb.Append("<a href=\"http://www.mywebsite.c om/mypage.aspx?cat egory=");
sb.Append(categ oryId);
sb.Append("&fil ter=price\">");
sb.Append(categ oryName);
sb.Append("</a>");
html += sb.ToString();
Does the embedded "&" character need special handling? Should I replace
"&" with "&" ? Or some other encoding ?
If this code is inside a method in a Page or web control, you can use the
method "UrlEncode" , so ...
=============== =============
string urlFormat =
"http://www.mywebsite.c om/mypage.aspx?cat egory={0}&filte r=price";
string tagFormat = "<a href=\"{0}\">{1 }</a>";
string url = Server.UrlEncod e(string.Format (urlFormat, categoryId));
html += string.Format(t agFormat, url, categoryName);
=============== =============
Also, using a StringBuilder for something like this is a bit of overkill
IMHO.
HTH ;)
Mythran