C# 将IHtmlContent/TagBuilder转换为C中的字符串#
我正在使用ASP.NET5。我需要将IHtmlContent转换为字符串C# 将IHtmlContent/TagBuilder转换为C中的字符串#,c#,asp.net,asp.net-core,asp.net-core-mvc,C#,Asp.net,Asp.net Core,Asp.net Core Mvc,我正在使用ASP.NET5。我需要将IHtmlContent转换为字符串 IIHtmlContent是ASP.NET 5Microsoft.AspNet.Html.Abstractions命名空间的一部分,是TagBuilder实现的接口 我有以下方法 public static IHtmlContent GetContent() { return new HtmlString("<tag>blah</tag>"); } 我得到了GetContent()的以下输
IIHtmlContent
是ASP.NET 5
Microsoft.AspNet.Html.Abstractions
命名空间的一部分,是TagBuilder
实现的接口
我有以下方法
public static IHtmlContent GetContent()
{
return new HtmlString("<tag>blah</tag>");
}
我得到了GetContent()的以下输出
而不是
<tag>blah</tag>
但它也附加相同的名称空间,而不是字符串值
我试着把它交给TagBuilder
TagBuilder content = (TagBuilder)GetContent();
但是TagBuilder没有转换为字符串的方法
如何将IHtmlContent或TagBuilder转换为字符串?如果只需将内容输出为字符串,只需添加此方法并将IHtmlContent对象作为参数传递即可获得字符串输出:
public static string GetString(IHtmlContent content)
{
using (var writer = new System.IO.StringWriter())
{
content.WriteTo(writer, HtmlEncoder.Default);
return writer.ToString();
}
}
对上述答案补充:
HtmlEncoder
的新实例在ASP.NET核心RTM中不起作用,因为Microsoft.Extensions.WebEncoders
命名空间已被删除,新的HtmlEncoder
类被移动到新命名空间System.Text.Encodings.Web
,但是这个类现在是作为一个抽象和密封的类编写的,所以您不能从中创建新的实例或派生类
将HtmlEncoder.Default
传递给该方法,它就会工作
public static string GetString(IHtmlContent content)
{
var writer = new System.IO.StringWriter();
content.WriteTo(writer, HtmlEncoder.Default);
return writer.ToString();
}
实际上,ASP.NET核心引入了一些谨慎的优化。如果要构建HTML扩展方法,则最有效的方法是避免使用字符串:
public static IHtmlContent GetContent(this IHtmlHelper helper)
{
var content = new HtmlContentBuilder()
.AppendHtml("<ol class='content-body'><li>")
.AppendHtml(helper.ActionLink("Home", "Index", "Home"))
.AppendHtml("</li>");
if(SomeCondition())
{
content.AppendHtml(@"<div>
Note `HtmlContentBuilder.AppendHtml()` is Mutable
as well as Fluent/Chainable.
</div>");
}
return content;
}
publicstaticihtmlcontent-GetContent(此IHtmlHelper帮助程序)
{
var content=new-HtmlContentBuilder()
.AppendHtml(“”)
.AppendHtml(helper.ActionLink(“Home”、“Index”、“Home”))
.AppendHtml(“ ”);
if(SomeCondition())
{
content.AppendHtml(@)
注意` HtmlContentBuilder.AppendHtml()`是可变的
以及流畅/可链接。
");
}
返回内容;
}
最后,在razor视图中,我们甚至不再需要
@Html.Raw(Html.GetContent())
(在ASP.NET MVC 5中曾经是必需的)-根据下面的LukášKmoch注释,这是无效的:ASP.NET MVC 5的类型是MvcHtmlString。您不需要使用Html.Raw()
只需调用@Html.GetContent()
就足够了,Razor将处理所有转义业务。有一个示例:
那么
到htmlstring
呢?另外,您从哪里获得IHtmlContent
呢?我指的是HtmlString
类的方法。您能给我指一下IHtmlContent
的文档吗?这似乎不在MSDNGood答案上。。这就是我要找的。在asp.vnext core上不起作用。使用HtmlEncoder.Default
代替StringWriter
是否应该使用块将包装在中?@bradlis7您是正确的。我已经更新了解决方案以反映您的建议。使用Razor内核,您只需使用@tagBuilderInstance
。我喜欢服务器端呈现的原因是,以牺牲性能为代价编写无聊的标记要快得多。为什么我们要将一个有序列表注入到标记中,以便能够呈现和锚定标记?这对SEO是不利的,并且会增加开发时间。ArrrghASP.NET MVC 5的类型为MvcHtmlString。您不需要使用Html.Raw()
public static string GetString(IHtmlContent content)
{
using (var writer = new System.IO.StringWriter())
{
content.WriteTo(writer, HtmlEncoder.Default);
return writer.ToString();
}
}
public static string GetString(IHtmlContent content)
{
var writer = new System.IO.StringWriter();
content.WriteTo(writer, HtmlEncoder.Default);
return writer.ToString();
}
public static IHtmlContent GetContent(this IHtmlHelper helper)
{
var content = new HtmlContentBuilder()
.AppendHtml("<ol class='content-body'><li>")
.AppendHtml(helper.ActionLink("Home", "Index", "Home"))
.AppendHtml("</li>");
if(SomeCondition())
{
content.AppendHtml(@"<div>
Note `HtmlContentBuilder.AppendHtml()` is Mutable
as well as Fluent/Chainable.
</div>");
}
return content;
}
public static string HtmlContentToString(IHtmlContent content, HtmlEncoder encoder = null)
{
if (encoder == null)
{
encoder = new HtmlTestEncoder();
}
using (var writer = new StringWriter())
{
content.WriteTo(writer, encoder);
return writer.ToString();
}
}