次のコードを使用しようとしています。
string myString = HttpServerUtility.HtmlEncode("my link & details");
次のエラーが発生します。
非静的フィールド、メソッド、またはプロパティにはオブジェクト参照が必要です。
HttpServerUtility.HtmlEncodeクラス内で使用できないのはなぜですか?
HttpUtilityに依存しない静的メソッドを持つ代わりに使用できますHttpContext。
string myString = HttpUtility.HtmlEncode("my link & details");
HtmlEncodeは静的メソッドではなく、HttpServerUtilityを呼び出すためのインスタンスが必要です。HttpContext.Current.ServerはHttpServerUtilityインスタンスであるため、代わりに;を使用できます。
string myString = HttpContext.Current.Server.HtmlEncode("my link & details");
.NET 4.5 を使用している場合、このユーティリティは System.Net.WebUtility の一部です。
string myString = System.Net.WebUtility.HtmlEncode(my link & details);