Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html encoding in .NET class library

I have a utility class for sending emails. The emails are generated as html.

I'm having problems with encoding special characters which aren't getting displayed correctly.

However, I can't work out how to use HtmlEncode as I don't have a current HttpContext. Is there a .Net class which will translate special characters to html codes without running under Asp.Net?

I hope that makes sense, I'm a bit flaky about how all this stuff works. Thanks!

like image 368
fearofawhackplanet Avatar asked Nov 19 '10 11:11

fearofawhackplanet


People also ask

What is HTML encoding C#?

HtmlEncode(Object)Converts an object's string representation into an HTML-encoded string, and returns the encoded string. public: static System::String ^ HtmlEncode(System::Object ^ value); C# Copy.

What is HTML encoding give example?

HTML encoding ensures that text will be correctly displayed in the browser, not interpreted by the browser as HTML. For example, if a text string contains a less than sign (<) or greater than sign (>), the browser would interpret these characters as an opening or closing bracket of an HTML tag.

What is the difference between HtmlEncode and Urlencode?

HTMLEncoding turns this character into "&lt;" which is the encoded representation of the less-than sign. URLEncoding does the same, but for URLs, for which the special characters are different, although there is some overlap. Save this answer. Show activity on this post.

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 &gt; .


1 Answers

I believe System.Net.WebUtility.HtmlEncode will provide this functionality. MSDN reference for WebUtility.HTMLEncode

like image 193
Dean Taylor Avatar answered Oct 15 '22 07:10

Dean Taylor