473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

building url with StringBuilder

I am using a StringBuilder to build a link tag (based on categoryId and
categoryName , which are populated elsewhere ) :

StringBuilder sb = new StringBuilder();
sb.Append("<a href=\"http://www.mywebsite.com/mypage.aspx?category=");
sb.Append(categoryId);
sb.Append("&filter=price\">");
sb.Append(categoryName);
sb.Append("</a>");
html += sb.ToString();

Does the embedded "&" character need special handling? Should I replace "&"
with "&amp;" ? Or some other encoding ?

Apr 20 '06 #1
3 11815

"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:el**************@TK2MSFTNGP05.phx.gbl...
I am using a StringBuilder to build a link tag (based on categoryId and
categoryName , which are populated elsewhere ) :

StringBuilder sb = new StringBuilder();
sb.Append("<a href=\"http://www.mywebsite.com/mypage.aspx?category=");
sb.Append(categoryId);
sb.Append("&filter=price\">");
sb.Append(categoryName);
sb.Append("</a>");
html += sb.ToString();

Does the embedded "&" character need special handling? Should I replace
"&" with "&amp;" ? 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.com/mypage.aspx?category={0}&filter=price";
string tagFormat = "<a href=\"{0}\">{1}</a>";
string url = Server.UrlEncode(string.Format(urlFormat, categoryId));
html += string.Format(tagFormat, url, categoryName);

============================

Also, using a StringBuilder for something like this is a bit of overkill
IMHO.

HTH ;)

Mythran

Apr 20 '06 #2
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.com/mypage.aspx?category={0}&filter=price";
string tagFormat = "<a href=\"{0}\">{1}</a>";
string url = Server.UrlEncode(string.Format(urlFormat, categoryId));
string html = string.Format(tagFormat, url, categoryName);

html = <a
href="http%3a%2f%2fwww.mywebsite.com%2fmypage.aspx %3fcategory%3d100%26filter%3dprice">toys
& games</a>
"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...

"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:el**************@TK2MSFTNGP05.phx.gbl...
I am using a StringBuilder to build a link tag (based on categoryId and
categoryName , which are populated elsewhere ) :

StringBuilder sb = new StringBuilder();
sb.Append("<a href=\"http://www.mywebsite.com/mypage.aspx?category=");
sb.Append(categoryId);
sb.Append("&filter=price\">");
sb.Append(categoryName);
sb.Append("</a>");
html += sb.ToString();

Does the embedded "&" character need special handling? Should I replace
"&" with "&amp;" ? 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.com/mypage.aspx?category={0}&filter=price";
string tagFormat = "<a href=\"{0}\">{1}</a>";
string url = Server.UrlEncode(string.Format(urlFormat, categoryId));
html += string.Format(tagFormat, url, categoryName);

============================

Also, using a StringBuilder for something like this is a bit of overkill
IMHO.

HTH ;)

Mythran

Apr 21 '06 #3

"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:u9**************@TK2MSFTNGP02.phx.gbl...
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.com/mypage.aspx?category={0}&filter=price";
string tagFormat = "<a href=\"{0}\">{1}</a>";
string url = Server.UrlEncode(string.Format(urlFormat, categoryId));
string html = string.Format(tagFormat, url, categoryName);

html = <a
href="http%3a%2f%2fwww.mywebsite.com%2fmypage.aspx %3fcategory%3d100%26filter%3dprice">toys
& games</a>


Oops :P

string url = string.Format(urlFormat, Server.UrlEncode(categoryId));

Basically, the goal is to encode any data in the querystring. So, you would
encode ... you should also encode categoryName as html using
Server.HtmlEncode, so that if this comes from a database, users can't enter
script (IE: <script language...) in categoryName...

HTH :)

Mythran

Apr 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
16
by: Alvin Bruney | last post by:
Is string builder intelligent enough to handle concats without behaving like string? Consider myStringBuilder.Append("one" + "two") what does the '+' do here? Because this syntax is also...
0
by: Mo | last post by:
I am having problem with marshaling struct in C#. //the original C++ struct typedef struct _tagHHP_DECODE_MSG { DWORD dwStructSize; // Size of decode structure. TCHAR ...
3
by: CodeRazor | last post by:
Hello, Simple question this one. How can I manually build a simple xmldocument in code. Lets say for example <Products> <Boat id="1"> <Price>9000</Price> </Boat>
4
by: Brian Shannon | last post by:
I have 3 combo boxes and two date text boxes on a .aspx page. The user can fill in any of the 5 controls or none to filter a datagrid. I was hoping someone could explain how to efficiently build...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
5
by: John Salerno | last post by:
Out of curiosity, is there any kind of equivalent in Python to the StringBuilder class in C#? Here's a quick description from the .NET documentation: "This class represents a string-like object...
34
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.