Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HtmlEncode in C#

Tags:

c#

Is there any way to html encode some text without using System.Web.HttpUtility.HtmlEncode method? I want to deploy my desktop application using .NET 3.5 Client Profile and the problem is that System.Web.dll it's not part of the .NET 3.5 Client Profile so I have to find an workaround.

like image 379
Cornel Avatar asked Oct 27 '09 15:10

Cornel


People also ask

What is the use of HtmlEncode?

The HTMLEncode method applies HTML encoding to a specified string. This is useful as a quick method of encoding form data and other client request data before using it in your Web application. Encoding data converts potentially unsafe characters to their HTML-encoded equivalent.

When should I use HtmlEncode?

Any time you are trying to output data that could include untrusted html, you should use HTMLENCODE . Encodes text and merge field values for use in HTML by replacing characters that are reserved in HTML, such as the greater-than sign ( > ), with HTML entity equivalents, such as > .

What does HttpUtility HtmlEncode do?

Converts an object's string representation into an HTML-encoded string, and returns the encoded string.

What is the difference between HtmlEncode and UrlEncode?

HtmlEncode – makes it safe to display user-entered text on a web page. < and > are turned into &lt; and &gt; UrlEncode – makes it safe to work as a url. is turned into + and a bunch more. “If you're wondering which one you should use in an HTTP POST, well just think of POST data as an extremely long query string.


1 Answers

I'm a fan of the AntiXSS library as well, but its worth mentioning that .net v4 includes a new utility class for encoding in System.dll. So if you have the option of moving to .net v4, you can use the client profile.

System.Net.WebUtility.HtmlEncode

like image 147
Joel Fillmore Avatar answered Sep 21 '22 00:09

Joel Fillmore