473,785 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.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 "&amp;" ? Or some other encoding ?

Apr 20 '06 #1
3 11857

"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
categoryName , 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 "&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.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

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.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 "&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.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

Apr 21 '06 #3

"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:u9******** ******@TK2MSFTN GP02.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.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>


Oops :P

string url = string.Format(u rlFormat, Server.UrlEncod e(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.HtmlEnco de, 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
4721
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, 20-30k), what are the performance differences (if any with something as trivial as this) between initializing a new instance of StringBuilder with a specified capacity vs. initializing a new instance without... (the final length is not fixed) ie,
16
410
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 legal but what is the cost compared to myStringBuilder.Append("one"); myStringBuilder.Append("two");
0
8773
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 pchMessage; // decoded message data TCHAR chCodeID; // AIM Id of symbology TCHAR chSymLetter; // HHP Id of symbology
3
7837
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
3806
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 the where clause of a sql string to send to SQL 2000 for a data set. Currenly I check each control with an IF statement to determine if something is filled in. If there is I begin building the where clause. Below is what I have done (and it...
9
9160
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 StringBuilder. At present I am porting a VB6 webclass app to VB.NET and therefore I am trying to make it as efficent as possible via the new functionality of VB.NET.
6
2785
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) using redim arrays to add to a normal byte array (2) using an ArrayList and finally (3) using a memorystream. These three classes are listed below the test sub called "TestBuildByteArray". It was interesting that using the memorystream was...
5
3881
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 whose value is a mutable sequence of characters. The value is said to be mutable because it can be modified once it has been created by appending, removing, replacing, or inserting characters." It's used for dealing with large strings that...
34
3559
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 200000 strings of 8 char each, string took over 25 minutes while StringBuilder took 40 milliseconds! Can anybody explain such a radical difference?
0
10350
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10097
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9957
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7505
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.