本文介绍了HtmlEncode在Asp.NET 5中在哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的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.WebMicrosoft.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接口( https://github.com/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 (https://github.com/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中在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 03:22