Server.HTMLEncode

M

Mark Rae

Hi,

The following is lifted straight from the MSDN help topic for
HttpServerUtility.HtmlEncode Method [C#]

---------------------------------------------------------------
The following example encodes a string for transmission by HTTP. It encodes
the string named TestString, which contains the text "This is a <Test
String>.", and copies it into the string named EncodedString as
"This+is+a+%3cTest+String%3e.".

[C#]
String TestString = "This is a <Test String>.";
StringWriter writer = new StringWriter();
Server.HtmlEncode(TestString, writer);
String EncodedString = writer.ToString();
---------------------------------------------------------------

However, when I run it, the value of EncodedString is "This is a &lt;Test
String&gt;." not "This+is+a+%3cTest+String%3e."

Am I missing something? Am I not using the correct encoding method?

Any assistance gratefully received.

Best,

Mark Rae
 
M

Mike Newton

Mark said:
Hi,

The following is lifted straight from the MSDN help topic for
HttpServerUtility.HtmlEncode Method [C#]

---------------------------------------------------------------
The following example encodes a string for transmission by HTTP. It encodes
the string named TestString, which contains the text "This is a <Test
String>.", and copies it into the string named EncodedString as
"This+is+a+%3cTest+String%3e.".

[C#]
String TestString = "This is a <Test String>.";
StringWriter writer = new StringWriter();
Server.HtmlEncode(TestString, writer);
String EncodedString = writer.ToString();
---------------------------------------------------------------

However, when I run it, the value of EncodedString is "This is a &lt;Test
String&gt;." not "This+is+a+%3cTest+String%3e."

Am I missing something? Am I not using the correct encoding method?

Any assistance gratefully received.

Best,

Mark Rae

Try Server.UrlEncode instead. HtmlEncode is generating HTML, as it
would be expected to.

-Mike
 
S

Shiva

Hi,

Use Server.UrlEncode for the output you are expecting.

Hi,

The following is lifted straight from the MSDN help topic for
HttpServerUtility.HtmlEncode Method [C#]

---------------------------------------------------------------
The following example encodes a string for transmission by HTTP. It encodes
the string named TestString, which contains the text "This is a <Test
String>.", and copies it into the string named EncodedString as
"This+is+a+%3cTest+String%3e.".

[C#]
String TestString = "This is a <Test String>.";
StringWriter writer = new StringWriter();
Server.HtmlEncode(TestString, writer);
String EncodedString = writer.ToString();
---------------------------------------------------------------

However, when I run it, the value of EncodedString is "This is a &lt;Test
String&gt;." not "This+is+a+%3cTest+String%3e."

Am I missing something? Am I not using the correct encoding method?

Any assistance gratefully received.

Best,

Mark Rae
 
M

Mark Rae

Mike & Silva,
Try Server.UrlEncode instead. HtmlEncode is generating HTML, as it
would be expected to.

Thanks very much. Is the MSDN topic wrong, then...?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top