I am trying to use the following code:
string myString = HttpServerUtility.HtmlEncode("my link & details");
I am getting the following error:
An object reference is required for the nonstatic field, method, or property.
Why can't I use HttpServerUtility.HtmlEncode
inside a class?
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 > .
1) They are the same. 2) It's a matter of convenience: Server. HtmlEncode() is readily availalble at runtime from a web page for example whereas HttpUtility. HtmlEncode() is a static method that can be used from anywhere.
HtmlEncode(Object) Converts an object's string representation into an HTML-encoded string, and returns the encoded string. HtmlEncode(String) Converts a string to an HTML-encoded string. HtmlEncode(String, TextWriter)
You can use HttpUtility
instead, which has a static method that does not depend on HttpContext
.
string myString = HttpUtility.HtmlEncode("my link & details");
More info on HttpUtility.HtmlEncode method on the MSDN.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With