简体   繁体   English

如何将编码标签呈现为正确的HTML而不是文本?

[英]How to render encoded tags as proper HTML, rather than text?

I'm getting the following text from a database: (supplied by client, so I can't do much with it) 我从数据库中获取以下文本:(由客户提供,所以我不能用它做多少)

investment professionals.<BR /><BR /> blah blah blah

which is getting rendered as: 它被渲染为:

investment professionals.<BR /><BR /> blah blah blah

I don't want to print the <BR /> tags on the screen. 我不想在屏幕上打印<BR />标签。 I want them to behave as actual breaks. 我希望他们表现得像实际的休息一样。

The following Html Helper code builds the span it exists in, adds that to a div and returns the HTML string: 以下Html Helper代码构建它所存在的范围,将其添加到div并返回HTML字符串:

StringBuilder sbElements = new StringBuilder();

TagBuilder span = new TagBuilder("span") {InnerHtml = subject.AboutText};
sbElements.Append(span.ToString());

TagBuilder div = new TagBuilder("div");
div.MergeAttribute("class", "about-text");
div.InnerHtml = sbElements.ToString();

return div.ToString();

If I Html.Encode() the output of the helper method, the encoded tags - /&gt;&lt; 如果我Html.Encode()帮助方法的输出,编码标签 - /&gt;&lt; - get written to the screen. - 写到屏幕上。 How can I take the source text I have and ensure that the tags get rendered as HTML, rather than text? 如何获取我的源文本并确保标记呈现为HTML而不是文本?

If you are using Razor, it will doubly-encode your string as @Chevex described. 如果您使用Razor,它将对您的字符串进行双重编码,如@Chevex所述。 If you use the new MVC3 <%: %> syntax, it will also doubly-encode it. 如果您使用新的MVC3 <%: %>语法,它也会对它进行双重编码。 Regardless of your view engine, you can work around the encoding with either the IHtmlString route (eg, MvcHtmlString) described by @Chevex or by bypassing the default encoding using a different template syntax. 无论您的视图引擎如何,您都可以使用@Chevex描述的IHtmlString路由(例如,MvcHtmlString)或使用不同的模板语法绕过默认编码来解决编码问题。

The later, which doesn't involve changing any code, just requires tweaking the syntax you use to render to a view. 后者不需要更改任何代码,只需要调整用于渲染视图的语法。

For Razor: 对于剃刀:

@Html.Raw(yourvariable)

For MVC's default view template system: 对于MVC的默认视图模板系统:

<%=yourvariable%>

Try this: 尝试这个:

    String Input = "investment professionals.&lt;BR /&gt;&lt;BR /&gt; blah blah blah";

    String Output = Server.HtmlDecode(Input);

Change return div.ToString() to MvcHtmlString.Create(div.ToString()) and the method return type to MvcHtmlString instead of a string . div.ToString()返回到MvcHtmlString.Create(div.ToString())并将方法返回类型改为MvcHtmlString而不是string This will prevent the view engine from automatically encoding the output. 这将阻止视图引擎自动编码输出。

Returning MvcHtmlString is the standard practice for rendering HTML output via helper methods. 返回MvcHtmlString是通过辅助方法呈现HTML输出的标准做法。

What is happening is that you are decoding the value from the database and the view engine is re-encoding it when it is rendered. 发生的事情是您正在解码数据库中的值,并且视图引擎在呈现时对其进行重新编码。 The Razor view engine automatically HTML encodes output in your views unless it is an MvcHtmlString . 除非是MvcHtmlString否则Razor视图引擎会自动对视图中的输出进行HTML编码。 Returning MvcHtmlString will stop that from happening. 返回MvcHtmlString将阻止它发生。

public static MvcHtmlString MyNonHtmlEncodedOutput(this HtmlHelper html)
{
        StringBuilder sbElements = new StringBuilder();

        TagBuilder span = new TagBuilder("span") {InnerHtml = Server.HtmlDecode(subject.AboutText)};
        sbElements.Append(span.ToString());

        TagBuilder div = new TagBuilder("div");
        div.MergeAttribute("class", "about-text");
        div.InnerHtml = sbElements.ToString();

        return MvcHtmlString.Create(div.ToString());
}

You can try this: 你可以试试这个:

 String Input = "&lt;p&gt;New Appartments is Avaliable at very Reasonable Price&lt;/p&gt;";
 String Output = Server.HtmlDecode(Input);
 string _output = System.Text.RegularExpressions.Regex.Replace(Output, "<.*?>", string.Empty);

You actually want to use Html.Decode(string) . 你实际上想要使用Html.Decode(string) This will convert encoded characters like &lt; 这将转换编码字符,如&lt; to < . <

Just to add my findings to this question as i was struggling to use × in this way. 只是将我的发现添加到这个问题,因为我正在努力以这种方式使用×。

SetInnerText() will html encode all text by default whereas InnerHtml doesn't encode anything as it is expecting raw html. 默认情况下,SetInnerText()将对所有文本进行html编码,而InnerHtml不会编码任何内容,因为它需要原始的html。

So if I have got this the right way around you would have to first decode your text string to get the un-encoded tags (ie <br/> ) and then set this into the InnerHtml property and the tags will be rendered as html. 因此,如果我有正确的方法,你必须首先解码你的文本字符串以获得未编码的标签(即<br/> ),然后将其设置为InnerHtml属性,标签将呈现为html。

so you would have something like 所以你会有类似的东西

element.InnerHtml = Server.HtmlDecode(yourText);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM