Thursday, March 22, 2012

Server.HTMLEncode

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 <Test
String>." 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 RaeHi,

Use Server.UrlEncode for the output you are expecting.

"Mark Rae" <mark@.mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:eBm3iJ$cEHA.592@.TK2MSFTNGP11.phx.gbl...
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 <Test
String>." 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
Mark Rae wrote:

> 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 <Test
> String>." 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
"Mike Newton" <MNewton@.Addus.com> wrote in message
news:ewkETQ$cEHA.2236@.TK2MSFTNGP10.phx.gbl...

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...?

0 comments:

Post a Comment