html在线encode,HttpServerUtility

对字符串进行 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

参数

要编码的文本字符串。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("");

}

}Public Class _Default

Inherits Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

Result.Text = Server.HtmlEncode("")

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("");

}

}Public Class SampleClass

Public Function GetEncodedText() As String

Return HttpContext.Current.Server.HtmlEncode("")

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 (), the browser would interpret these characters as the opening or closing bracket of an HTML tag. 如果字符是 HTML 编码的,则将其转换为字符串 < 和 > ,这将导致浏览器显示小于号并正确地显示大于号。When the characters are HTML encoded, they are converted to the strings < and >, which causes the browser to display the less than sign and greater than sign correctly.

此方法是 HttpUtility.HtmlEncode 在运行时从 ASP.NET 应用程序访问方法的一种简便方法。This method is a convenient way to access the HttpUtility.HtmlEncode method at run time from an ASP.NET application. 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.

适用于

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值