自定义 HtmlHelper 呈现文本而不是标记

发布于 2024-11-28 01:24:25 字数 631 浏览 1 评论 0原文

我写了一个基本的HtmlHelper。这是我编写的一个测试,它简化了我正在做的事情,并确保当我使用 StringBuilderTagBuilder 对象时会发生这种情况:

public static string HelloWorld(this HtmlHelper htmlHelper, string name)
{
    var sb = new StringBuilder();
    var builder = new TagBuilder("span");
    builder.InnerHtml = "Hello, " + name + "!";
    sb.Append(builder.ToString(TagRenderMode.Normal));
    sb.Append("<br/>");

    return sb.ToString();
}

此控件的输出对字符串进行编码所以我看到以下内容: 你好,Jason!

在我看来,我正在使用以下内容:@Html.HelloWorld("Jason")< /code>

有什么想法我可能做错了吗?

I have written a basic HtmlHelper. Here's a test I wrote that simplifies what I was doing and to provide self assurance that it is happening when I use StringBuilder and TagBuilder objects:

public static string HelloWorld(this HtmlHelper htmlHelper, string name)
{
    var sb = new StringBuilder();
    var builder = new TagBuilder("span");
    builder.InnerHtml = "Hello, " + name + "!";
    sb.Append(builder.ToString(TagRenderMode.Normal));
    sb.Append("<br/>");

    return sb.ToString();
}

The output for this control encodes the string so I see the following:
<span>Hello, Jason!</span><br/>

In my view, I'm using the following: @Html.HelloWorld("Jason")

Any ideas what I can be doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

木森分化 2024-12-05 01:24:25

默认情况下,Razor 引擎会在其呈现的任何字符串中转义 HTML。如果将返回类型更改为 IHtmlString,则不会发生这种情况。

public static IHtmlString HelloWorld(this HtmlHelper htmlHelper, string name)
    ...
    return new HtmlString(sb.ToString());
}

The Razor engine escapes HTML by default in any strings that it renders. If you change the return type to IHtmlString, this won't happen.

public static IHtmlString HelloWorld(this HtmlHelper htmlHelper, string name)
    ...
    return new HtmlString(sb.ToString());
}
旧情别恋 2024-12-05 01:24:25

您的静态方法必须是 HtmlString 而不是 string,并且您必须返回 HtmlString(sb.ToString());

public static HtmlString HelloWorld(this HtmlHelper htmlHelper, string name)
{
    var sb = new StringBuilder();
    var builder = new TagBuilder("span");
    builder.InnerHtml = "Hello, " + name + "!";
    sb.Append(builder.ToString(TagRenderMode.Normal));
    sb.Append("<br/>");

    return new HtmlString(sb.ToString());
}

Your static method must be HtmlString instead of string, and you must return the HtmlString(sb.ToString());

public static HtmlString HelloWorld(this HtmlHelper htmlHelper, string name)
{
    var sb = new StringBuilder();
    var builder = new TagBuilder("span");
    builder.InnerHtml = "Hello, " + name + "!";
    sb.Append(builder.ToString(TagRenderMode.Normal));
    sb.Append("<br/>");

    return new HtmlString(sb.ToString());
}
迟月 2024-12-05 01:24:25

使用 AppendFormat 而不是 Append

var sb = new StringBuilder();
    var builder = new TagBuilder("span");
    builder.InnerHtml = "Hello, " + name + "!";
    sb.AppendFormat(builder.ToString(TagRenderMode.Normal));
    sb.AppendFormat("<br/>");

    return sb.ToString();

Use AppendFormat instead of Append

var sb = new StringBuilder();
    var builder = new TagBuilder("span");
    builder.InnerHtml = "Hello, " + name + "!";
    sb.AppendFormat(builder.ToString(TagRenderMode.Normal));
    sb.AppendFormat("<br/>");

    return sb.ToString();
对你的占有欲 2024-12-05 01:24:25

这是 2018 年使用 .Net Core 2 的答案

using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.IO;

public static IHtmlContent HelloWorld(this IHtmlHelper html, string name)
{
    var span = new TagBuilder("span");
    span.InnerHtml.Append("Hello, " + name + "!");

    var br = new TagBuilder("br") {TagRenderMode = TagRenderMode.SelfClosing};

    string result;

    using (var writer = new StringWriter())
    {
        span.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default);
        br.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default);
        result = writer.ToString();
    }

    return new HtmlString(result);
}

Here is an answer for the year 2018 working with .Net Core 2

using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.IO;

public static IHtmlContent HelloWorld(this IHtmlHelper html, string name)
{
    var span = new TagBuilder("span");
    span.InnerHtml.Append("Hello, " + name + "!");

    var br = new TagBuilder("br") {TagRenderMode = TagRenderMode.SelfClosing};

    string result;

    using (var writer = new StringWriter())
    {
        span.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default);
        br.WriteTo(writer, System.Text.Encodings.Web.HtmlEncoder.Default);
        result = writer.ToString();
    }

    return new HtmlString(result);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文