Skip to content

Category: .NET

Format an HTML Safe String using .NET

I’m posting this as much as a reminder to myself as anything. I infrequently find myself needing to make sure that a string is safe to use on the web in any number of scenarios. The .Net framework contains a function to do just that, but they’ve cleverly hidden it away in the security assembly. That’s why I needed to write this – I use it infrequently enough that I can never remember where it is.

To use it, just make a call to:

System.Security.SecurityElement.Escape("text to encode")

And that will do the trick "&" becomes “&amp;”, “<” becomes “&lt”;, etc.

Leave a Comment