0

I am new to a lot of the ASP.net MVC5 features. I wanted to know if there as a library that would automatically add the escape characters to a C# string that's intended to be output as html? Would HTMLAgility Pack be what I am looking for? Or is there soemthing built in that does it for you? May someone please let me know if I am using the right terminology?

var fileContents = System.IO.File.ReadAllText(Server.MapPath("~/Static/email.txt"))

the file contents would be:

<table width="600" cellpadding="0" cellspacing="0" style="background-color:black;">

the library would convert "filecontents" to:

 "<table width=\"600\" cellpadding=\"0\" cellspacing=\"0\" style=\"background-color:black;\">\n"
Thanos Markou
  • 2,587
  • 3
  • 25
  • 32
Jesse
  • 785
  • 2
  • 9
  • 29
  • Why would you want to do that? An actual backslash in a string doesn't mean anything special, except when written as an actual literal in code. The string you are reading from the file is valid HTML and should be able to be used without modification. In fact, adding backslashes and extra quotes to it will turn it into _not_ valid HTML. – Peter Duniho Jul 09 '15 at 06:26
  • http://stackoverflow.com/questions/4281424/writing-outputting-html-strings-unescaped there you have a lots of ways to do it – Thorarins Jul 09 '15 at 06:26
  • I want to load the html so I can send an e-mail basically. That's why I need it to be escaped. So it can be pushed to MailMessage. I want to load the email with out having to go and escape each email by concatenating strings. – Jesse Jul 09 '15 at 06:29
  • 1
    Your using MVC, you should use Razor for your email templates, much better. You can transform a razor view to a text string, then send that in the email. http://stackoverflow.com/questions/15371540/how-to-convert-razor-view-to-string – Ryan Mann Jul 09 '15 at 14:20

2 Answers2

4
System.Web.HttpUtility.HtmlEncode("<p>I am some html</p>");
Ryan Mann
  • 5,178
  • 32
  • 42
1

Output string to html, you can try to use HttpServerUtility.HtmlEncode

HarryQuake
  • 163
  • 1
  • 13