HttpServerUtility.HtmlEncode 方法

定义

对要在浏览器中显示的字符串进行编码。Encodes a string to be displayed in a browser.

若要对 Web 应用程序之外的值进行编码或解码,请使用 WebUtility 类。To encode or decode values outside of a web application, use the WebUtility class.

重载

HtmlEncode(String)

对字符串进行 HTML 编码并返回已编码的字符串。HTML-encodes a string and returns the encoded string.

HtmlEncode(String, TextWriter)

对字符串进行 HTML 解码,并将结果输出发送到 TextWriter 输出流。HTML-encodes a string and sends the resulting output to a TextWriter output stream.

HtmlEncode(String)

对字符串进行 HTML 编码并返回已编码的字符串。HTML-encodes a string and returns the encoded string.

public:
 System::String ^ HtmlEncode(System::String ^ s);
public string HtmlEncode (string s);
member this.HtmlEncode : string -> string
Public Function HtmlEncode (s As String) As String

参数

s
String

要编码的文本字符串。The text string to encode.

返回

HTML 编码的文本。The HTML-encoded text.

示例

下面的示例演示如何对可能对不安全代码进行编码的值进行 HTML 编码。The following example shows how to HTML-encode a value that potentially codes unsafe code. 代码驻留在网页的代码隐藏文件中。The code resides in the code-behind file for a web page. 要编码的值在此示例中为硬编码, 这只是为了简化示例, 并显示可以进行 HTML 编码的值的类型。The value to encode is hard-coded in this example only to simplify the example and show the type of value you might HTML-encode. 通常, 您需要对从用户或请求收到的值进行 HTML 编码。Typically, you would HTML-encode a value that you received from the user or the request. Result``Literal引用控件。Result refers to a Literal control.

public partial class _Default : Page
{        
    protected void Page_Load(object sender, EventArgs e)
    {
        Result.Text = Server.HtmlEncode("<script>unsafe</script>");      
    } 
}
Public Class _Default
    Inherits Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Result.Text = Server.HtmlEncode("<script>unsafe</script>")
    End Sub
End Class

下面的示例与前面的示例类似, 只不过它演示了如何对不在代码隐藏文件中的类中的值进行 HTML 编码。The next example is similar to the previous example except it shows how to HTML-encode a value from within a class that is not in the code-behind file.

public class SampleClass
{
    public string GetEncodedText()
    {
        return HttpContext.Current.Server.HtmlEncode("<script>unsafe</script>");
    }
}
Public Class SampleClass
    Public Function GetEncodedText() As String
        Return HttpContext.Current.Server.HtmlEncode("<script>unsafe</script>")
    End Function
End Class

注解

HTML 编码可确保文本在浏览器中正确显示, 并且浏览器不会将其解释为 HTML 格式。HTML encoding makes sure that text is displayed correctly in the browser and not interpreted by the browser as HTML. 例如, 如果文本字符串包含小于号 (<) 或大于号 (>), 则浏览器会将这些字符解释为 HTML 标记的左括号或右括号。For example, if a text string contains a less than sign (<) or greater than sign (>), the browser would interpret these characters as the opening or closing bracket of an HTML tag. 如果字符是 HTML 编码的, 则将其转换为字符串&lt;&gt;, 这将导致浏览器显示小于号并正确地显示大于号。When the characters are HTML encoded, they are converted to the strings &lt; and &gt;, which causes the browser to display the less than sign and greater than sign correctly.

此方法是在运行时从 ASP.NET 应用HttpUtility.HtmlEncode程序访问方法的一种简便方法。This method is a convenient way to access the HttpUtility.HtmlEncode method at run time from an ASP.NET application. 此方法在内部使用HttpUtility.HtmlEncode来对字符串进行编码。Internally, this method uses HttpUtility.HtmlEncode to encode strings.

在 ASP.NET 网页的代码隐藏文件中, HttpServerUtility Server通过属性访问类的实例。In the code-behind file for an ASP.NET web page, access an instance of the HttpServerUtility class through the Server property. 在不在代码隐藏文件中的类中, 使用HttpContext.Current.Server访问HttpServerUtility类的实例。In a class that is not in a code-behind file, use HttpContext.Current.Server to access an instance of the HttpServerUtility class.

在 web 应用程序之外, 使用WebUtility类对值进行编码或解码。Outside of a web application, use the WebUtility class to encode or decode values.

HtmlEncode(String, TextWriter)

对字符串进行 HTML 解码,并将结果输出发送到 TextWriter 输出流。HTML-encodes a string and sends the resulting output to a TextWriter output stream.

public:
 void HtmlEncode(System::String ^ s, System::IO::TextWriter ^ output);
public void HtmlEncode (string s, System.IO.TextWriter output);
member this.HtmlEncode : string * System.IO.TextWriter -> unit
Public Sub HtmlEncode (s As String, output As TextWriter)

参数

s
String

要编码的字符串。The string to encode.

output
TextWriter

TextWriter 输出包含已编码字符串的流。The TextWriter output stream that contains the encoded string.

示例

下面的示例对字符串进行编码以便通过 HTTP 传输。The following example encodes a string for transmission by HTTP. 它对名为TestString的字符串进行编码, 该字符串包含文本 "this is a <test string >.", 并将其复制EncodedString到名为 "this <is a>test string" 的字符串中。It encodes the string named TestString, which contains the text "This is a <Test String>.", and copies it into the string named EncodedString as "This is a <Test String>.".

String TestString = "This is a <Test String>.";
StringWriter writer = new StringWriter();
Server.HtmlEncode(TestString, writer);
String EncodedString = writer.ToString();
   
Dim TestString As String = "This is a <Test String>."
Dim writer As New StringWriter
Server.HtmlEncode(TestString, writer)
Dim EncodedString As String = writer.ToString()
   

注解

HTML 编码可确保文本在浏览器中正确显示, 而不会被浏览器解释为 HTML 格式。HTML encoding ensures that text will be correctly displayed in the browser, not interpreted by the browser as HTML. 例如, 如果文本字符串包含小于号 (<) 或大于号 (>), 则浏览器会将这些字符解释为 HTML 标记的左括号或右括号。For example, if a text string contains a less than sign (<) or greater than sign (>), the browser would interpret these characters as an opening or closing bracket of an HTML tag. 这两个字符的 HTML 编码分别&lt;&gt;和, 这将导致浏览器显示小于号并正确地显示大于号。The HTML encoding of these two characters is &lt; and &gt;, respectively, which causes the browser to display the less than sign and greater than sign correctly.

HtmlEncode是在运行时从 ASP.NET 应用HttpUtility.HtmlEncode程序访问方法的一种简便方法。HtmlEncode is a convenient way to access the HttpUtility.HtmlEncode method at run time from an ASP.NET application. 在内部HtmlEncode , HttpUtility.HtmlEncode使用对字符串进行编码。Internally, HtmlEncode uses HttpUtility.HtmlEncode to encode strings.

若要对 Web 应用程序之外的值进行编码或解码,请使用 WebUtility 类。To encode or decode values outside of a web application, use the WebUtility class.

适用于