Showing posts with label htmlencode. Show all posts
Showing posts with label htmlencode. Show all posts

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

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

Server.HtmlEncode DataBinder

Can anyone help with Server.HtmlEncode encoding this?

'<%# DataBinder.Eval(Container.DataItem, "CAT_ID",
"pageview.aspx?tab=1&catid={0}") %
I've tried using similar to this:
<%# Server.HtmlEncode(DataBinder.Eval(Container,
"DataItem.Field1").ToString()) %
but get error message BC30455: Argument not specified for parameter '

Many thanks'<%#"/admin/managenews.aspx?action=2&id="+DataBinder.Eval(Container.DataItem,"NID")%>'
this may help u
"Fossie" <gillmcd@.ihug.co.nz> wrote in message
news:1148111001.439320.296410@.38g2000cwa.googlegro ups.com...
> Can anyone help with Server.HtmlEncode encoding this?
> '<%# DataBinder.Eval(Container.DataItem, "CAT_ID",
> "pageview.aspx?tab=1&catid={0}") %>
> I've tried using similar to this:
> <%# Server.HtmlEncode(DataBinder.Eval(Container,
> "DataItem.Field1").ToString()) %>
> but get error message BC30455: Argument not specified for parameter '
> Many thanks
Thanks - I tried the + and got this error message:

Conversion from string "pageview.aspx?tab=1&catid=1" to type 'Double'
is not valid.
'<%#"/pageview.aspx?tab=1&catid="+DataBinder.Eval(Container.DataItem,"CAT_ID")%>'
"Fossie" <gillmcd@.ihug.co.nz> wrote in message
news:1148112901.230750.47520@.j73g2000cwa.googlegro ups.com...
> Thanks - I tried the + and got this error message:
> Conversion from string "pageview.aspx?tab=1&catid=1" to type 'Double'
> is not valid.
<asp:HyperLink id="hyperlinkk1" Runat="server"
NavigateUrl='<%#"/pageview.aspx?tab=1&catid="+DataBinder.Eval(Container.DataItem,"CAT_ID")%>'</asp:HyperLink>
"Fossie" <gillmcd@.ihug.co.nz> wrote in message
news:1148112901.230750.47520@.j73g2000cwa.googlegro ups.com...
> Thanks - I tried the + and got this error message:
> Conversion from string "pageview.aspx?tab=1&catid=1" to type 'Double'
> is not valid.

Server.HTMLEncode and the # (pound) symbol

Can someone tell me why HTMLEncode doesn't convert # to %23?

I am building a querystring and the # comes through as a #, which of
course kills the rest of the items in the querystring.

Any idea? Is there another function I can use? For now I just wrote
my own to swap out any #s.query strings should not be HTML encoded, but rather URL encoded. try

HttpUtility.UrlEncode

-- bruce (sqlwork.com)

"Chris" <cmay@.walshgroup.com> wrote in message
news:44f6e099.0407130720.5cbcd001@.posting.google.c om...
> Can someone tell me why HTMLEncode doesn't convert # to %23?
> I am building a querystring and the # comes through as a #, which of
> course kills the rest of the items in the querystring.
> Any idea? Is there another function I can use? For now I just wrote
> my own to swap out any #s.
Holy crap, I can't believe that I didn't realize what I was doing.

I knew something wasn't right... I am a disgrace :)

Thanks!

"bruce barker" <nospam_brubar@.safeco.com> wrote in message news:<#83EIDPaEHA.1152@.TK2MSFTNGP09.phx.gbl>...
> query strings should not be HTML encoded, but rather URL encoded. try
> HttpUtility.UrlEncode
> -- bruce (sqlwork.com)
>
> "Chris" <cmay@.walshgroup.com> wrote in message
> news:44f6e099.0407130720.5cbcd001@.posting.google.c om...
> > Can someone tell me why HTMLEncode doesn't convert # to %23?
> > I am building a querystring and the # comes through as a #, which of
> > course kills the rest of the items in the querystring.
> > Any idea? Is there another function I can use? For now I just wrote
> > my own to swap out any #s.

Server.HTMLEncode and the # (pound) symbol

Can someone tell me why HTMLEncode doesn't convert # to %23?
I am building a querystring and the # comes through as a #, which of
course kills the rest of the items in the querystring.
Any idea? Is there another function I can use? For now I just wrote
my own to swap out any #s.query strings should not be HTML encoded, but rather URL encoded. try
HttpUtility.UrlEncode
-- bruce (sqlwork.com)
"Chris" <cmay@.walshgroup.com> wrote in message
news:44f6e099.0407130720.5cbcd001@.posting.google.com...
> Can someone tell me why HTMLEncode doesn't convert # to %23?
> I am building a querystring and the # comes through as a #, which of
> course kills the rest of the items in the querystring.
> Any idea? Is there another function I can use? For now I just wrote
> my own to swap out any #s.
Holy crap, I can't believe that I didn't realize what I was doing.
I knew something wasn't right... I am a disgrace :)
Thanks!
"bruce barker" <nospam_brubar@.safeco.com> wrote in message news:<#83EIDPaEHA.1152@.TK2MSFTNG
P09.phx.gbl>...
> query strings should not be HTML encoded, but rather URL encoded. try
> HttpUtility.UrlEncode
> -- bruce (sqlwork.com)
>
> "Chris" <cmay@.walshgroup.com> wrote in message
> news:44f6e099.0407130720.5cbcd001@.posting.google.com...

server.htmlencode method

hi to all,

As we know that the server.htmlencode method makes html encodeing so what i understand hat if i have a text like that <b>Test string</b>

then when i apply the server.htmlencode method on that text then the output will be :

<b>Test String</b>

and the output after applying this method will be displayed on a Web browser as:

<b>Test string</b>

so my question why we directly send the text <b>Test string</b> to the web browser without using the server.htmlencode method to make encoding?

so ineed some clarification on that topic.

your help is highly appreciated

Best regards

wissam1:

why we directly send the text <b>Test string</b> to the web browser without using the server.htmlencode method to make encoding?

If you do not encode, it will behave as markup (you would see a bolden string in your sample), with all security concerns eventually involved. If instead you do encode, it will render as a plain string.

That's all i guess.

HTH. -LV


HI

HtmlEncoding is done mostly to avoid XSS (Cross Site Scripting) attacks.
Google for more on that.!

Thanks

Server.HtmlEncode DataBinder

Can anyone help with Server.HtmlEncode encoding this?
'<%# DataBinder.Eval(Container.DataItem, "CAT_ID",
"pageview.aspx?tab=1&catid={0}") %>
I've tried using similar to this:
<%# Server.HtmlEncode(DataBinder.Eval(Container,
"DataItem.Field1").ToString()) %>
but get error message BC30455: Argument not specified for parameter '
Many thanks'<%#"/admin/managenews.aspx?action=2&id="+DataBinder.Eval(Container.DataItem
,"NID")%>'
this may help u
"Fossie" <gillmcd@.ihug.co.nz> wrote in message
news:1148111001.439320.296410@.38g2000cwa.googlegroups.com...
> Can anyone help with Server.HtmlEncode encoding this?
> '<%# DataBinder.Eval(Container.DataItem, "CAT_ID",
> "pageview.aspx?tab=1&catid={0}") %>
> I've tried using similar to this:
> <%# Server.HtmlEncode(DataBinder.Eval(Container,
> "DataItem.Field1").ToString()) %>
> but get error message BC30455: Argument not specified for parameter '
> Many thanks
>

Server.HTMLEncode/Decode and Databinding

I am using Server.HTMLEncode on user input prior to storing it in an sql table, and of course I use Server.HTMLDecode before displaying it or presenting the data back to the user for editing. HOWEVER, when I bind the data to a dropdownlist or similar control it is not "HTML decoded" and chars like & amp ; appear. My question is: is there anyway around this other then using a for next loop (and decoding selected fields) to get the data into the dropdown list. ThanksMy question would be why would you html encode before sending to the database and decode before sending to the client?
bleroy, maybe this is his reason:

Use Server.HTMLEncode for strings stored in a database

If your ASP pages display strings stored in database fields, you should always process the strings with the Server.HTMLEncode method, otherwise the string won't be displayed correctly in the user's browser if it contains characters that have a special meaning to HTML, such as the quote ("), the less-than (<) and greater-than (>) symbols, the ampersand symbol (&), and any character whose ANSI code is larger than 127.
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q253119

recommends using Server.HTMLEncode from forms and querystrings:

HOWTO: Review ASP Code for CSSI Vulnerability

This article describes how to identify and correct Active Server Pages (ASP) applications that are susceptible to Cross-Site Scripting Security Issues (CSSI). Only input that is not correctly validated or formatted makes your application vulnerable to attack.
You both are beside the point here.

Encoding before sending TO the database and decoding what comes FROM the database is absolutely useless. The database won't suffer from any javascript injections, believe me. Sql injections, perhaps, but X-site scripting, no way.

These papers explain how you should encode any data FROM THE USER TO THE PAGE.

So if you're storing user data to the database, you may encode it at this point or when fetching it back from the database, but don't DECODE what comes from the database before sending it to the client.

It's the "and of course [?] I use Server.HTMLDecode before displaying it or presenting the data back to the user for editing" from the original post that is utterly wrong.
> Encoding before sending TO the database and decoding what comes FROM the database is absolutely useless.

> These papers explain how you should encode any data FROM THE USER TO THE PAGE.

What is stored in the database (from the user) will be displayed on the page. Is is still wrong?

Server.HtmlEncode(createText.Text) vs. HttpUtility.HtmlEncode(createText.Text);

What is the difference? and which one should I use?

Server.HtmlEncode(createText.Text)

vs.

HttpUtility.HtmlEncode(createText.Text)

Server.HtmlEncode calls HttpUtility.HtmlEncode.


According toReflector, Server is an instance ofHttpServerUtility

Public ReadOnly Property Server As HttpServerUtility
Get
If (Me._server Is Nothing) Then
Me._server = New HttpServerUtility(Me)
End If
Return Me._server
End Get
End Property

And as noted HttpServerUtility.HtmlEncode makes a straight call to the shared method HttpUtility.HtmlEncode

Public Sub HtmlEncode(ByVal s As String, ByVal output As TextWriter)
HttpUtility.HtmlEncode(s, output)
End Sub

So for best performance, you could call directly to HttpUtility.HtmlEncode yourself.

Server.HtmlEncode(createText.Text) vs. HttpUtility.HtmlEncode(createText.Text)

Server.HtmlEncode(createText.Text) vs. HttpUtility.HtmlEncode(createText.Text);

From thedocumentation:

Server.HtmlEncode is a convenient way to access theSystem.Web.HttpUtility.HtmlEncode method at run time from an ASP.NET Web application. Internally,HtmlEncode usesSystem.Web.HttpUtility.HtmlEncode to encode strings.