Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use HttpServerUtility.HtmlEncode inside a class?

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?

like image 828
markzzz Avatar asked Apr 21 '12 16:04

markzzz


People also ask

When to 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 > .

What is the difference between server HtmlEncode and HttpUtility HtmlEncode?

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.

What is HtmlEncode C#?

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)


1 Answers

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.

like image 110
jorgebg Avatar answered Oct 11 '22 19:10

jorgebg