How do I encode an URL /HTML / Text in .NET?

By axostech

Server.HtmlEncode(strValue); is the answer.. Unless..

You want to call it from a static method (and we always do… ;)

We have two main options:

System.Web.HttpContext.Current.Server.HtmlEncode(s trValue);

And thenĀ  System.Web.HttpUtility.HtmlEncode (my preferred method..) Why?

Because it also has methods to encode Url’s and you can get the data as a string, or byte array (nice for marshalling or persisting to a data store.. )

Leave a Reply