[英]Write a new line to a web site using System.Web.HttpUtility.HtmlEncode in C#
I'm trying to write some text to a website using the System.Web.HttpUtility.HtmlEncode function in C#. 我正在尝试使用C#中的System.Web.HttpUtility.HtmlEncode函数将一些文本写入网站。 The string parameter that I give to the function has some Environment.Newline's in it, but they are not written out on the website. 我为该函数提供的字符串参数中包含一些Environment.Newline,但它们并未写在网站上。 Does anyone know why this is and how I can fix it. 有谁知道这是为什么以及我如何解决它。 Thanks in advance. 提前致谢。
Newline is written out as a physical line break so you will have to either wrap in a pre: 换行符是作为物理换行符写出的,因此您必须先包装一个换行符:
response.Write("<pre>" + HttpUtility.HtmlEncode(str) + "</pre>");
Or replace the new line with a BR AFTER you have HtmlEncoded (or it'll encode the BR as well): 或用HtmlEncoded后的BR替换新行(否则它将对BR进行编码):
response.Write(HttpUtility.HtmlEncode(str).Replace("\n", "<br />"));
Given its HTML NewLine characters do not display as whitespace. 鉴于其HTML NewLine字符不会显示为空格。 Try replacing your new line characters with <br/>
elements. 尝试用<br/>
元素替换换行符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.