11

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.

0

4 Answers 4

15

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"
}
Sign up to request clarification or add additional context in comments.

Comments

11

HtmlEncoder in Microsoft.Extensions.WebEncoders.Core is just a wrapper around System.Text.Encodings.Web.HtmlEncoder to implement IHtmlEncoder interface (https://github.com/aspnet/HttpAbstractions/blob/release/src/Microsoft.Extensions.WebEncoders.Core/HtmlEncoder.cs).

You can take Microsoft.Extensions.WebEncoders.HtmlEncoder.Default and pass to WriteTo method.

From what I see in dev branch MVC moved to using System.Text.Encodings.Web.HtmlEncoder directly so you wan't need to use Microsoft.Extensions.WebEncoders.HtmlEncoder anymore in future.

3 Comments

Thanks! I wish there were some consolidated view of the asp.net 5 source so you locate types easier. :-)
you can add this dependencie for rc2 : "System.Text.Encodings.Web": "4.0.0-rc2-24027"
the link is now dead
5

As for RC1 update 1, here is how it is done:

using System.Text.Encodings.Web;

...

HtmlEncoder.Default.Encode("...");

Comments

4

Just use System.Net.WebUtility.HtmlEncode or decode as:

System.Net.WebUtility.HtmlEncode() System.Net.WebUtility.HtmlDecode()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.