我有一个自定义的IHtmlHelper扩展方法,该方法使用TagBuilder并返回HtmlString.我不能再将tagBuiler.ToString()传递给HtmlString构造函数,因为它现在只返回类型名称.
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.
我知道可以使用tabBuiler.WriteTo(TextWriter, IHtmlEncoder)方法,但是我不知道如何真正使用实现IHtmlEncoder的对象.我在System.Text.Encodings.Web和Microsoft.Framework.WebEncoders中都看到了编码器.但是两个命名空间中的类型似乎不能很好地配合使用.
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.
推荐答案HtmlEncoder只是System.Text.Encodings.Web.HtmlEncoder的包装,用于实现IHtmlEncoder接口( github/aspnet/HttpAbstractions/blob/release/src/Microsoft. Extensions.WebEncoders.Core/HtmlEncoder.cs ).
HtmlEncoder in Microsoft.Extensions.WebEncoders.Core is just a wrapper around System.Text.Encodings.Web.HtmlEncoder to implement IHtmlEncoder interface (github/aspnet/HttpAbstractions/blob/release/src/Microsoft.Extensions.WebEncoders.Core/HtmlEncoder.cs).
您可以采用Microsoft.Extensions.WebEncoders.HtmlEncoder.Default并传递给WriteTo方法.
You can take Microsoft.Extensions.WebEncoders.HtmlEncoder.Default and pass to WriteTo method.
根据我在dev分支中看到的内容,MVC转而直接使用System.Text.Encodings.Web.HtmlEncoder,因此您以后不再需要使用Microsoft.Extensions.WebEncoders.HtmlEncoder.
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.
更多推荐
HtmlEncode在Asp.NET 5中在哪里
发布评论