I have a custom IHtmlHelper
extension method that uses TagBuilder
and returns an HtmlString
. I can no longer pass tagBuiler.ToString()
to the HtmlString
constructor as that just returns the typename now.
I see I can use the tabBuiler.WriteTo(TextWriter, IHtmlEncoder)
method but I don't know exactly how to get my hands on an object that implments IHtmlEncoder
. I see encoders in both System.Text.Encodings.Web
and Microsoft.Framework.WebEncoders
. But the types in the two namespace don't seem to play well together.
HtmlEncode is a convenient way to access the HttpUtility. HtmlEncode method at run time from an ASP.NET application. Internally, HtmlEncode uses HttpUtility. HtmlEncode to encode strings. To encode or decode values outside of a web application, use the WebUtility class.
HtmlEncode(Object)Converts an object's string representation into an HTML-encoded string, and returns the encoded string. public: static System::String ^ HtmlEncode(System::Object ^ value); C# Copy.
The HTMLEncode method applies HTML encoding to a specified string. This is useful as a quick method of encoding form data and other client request data before using it in your Web application. Encoding data converts potentially unsafe characters to their HTML-encoded equivalent.
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 > .
In ASP.NET Core RC2 you can find HtmlDecode
in System.Net.WebUtility
:
In your project.json import dependency system.net.utilities
"dependencies": {
"System.Text.Encodings.Web": "4.0.0-rc2-24027"
}
Just use System.Net.WebUtility.HtmlEncode or decode as:
System.Net.WebUtility.HtmlEncode() System.Net.WebUtility.HtmlDecode()
As for RC1 update 1, here is how it is done:
using System.Text.Encodings.Web;
...
HtmlEncoder.Default.Encode("...");
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