当前位置:主页   - 电脑 - 网站开发 - ASP.Net
ASP.NET中的HTML编码和URL编码
来源:网络   作者:    更新时间:2010-09-26
收藏此页】    【字号    】    【打印】    【关闭

导 读:Server.HTMLEncode 和 Server.URLEncode 是ASP中很常用的函数,在ASP.NET中也有类似的函数:HtmlEncode 和 UrlEncode (注意大小写)以下用实例来进行介绍。

Server.HTMLEncode and Server.URLEncode are very common functions used
by ASP developers. Well, as you may have guessed, these two functions
have ASP.NET counterparts. HtmlEncode and UrlEncode (notice case) are
part of the System.Web.HttpUtility namespace.

Here is the demo

urlencode.asp?test=1&test1=2
urlencode.asp%3ftest%3d1%26test1%3d2


This is a test & hopefully it works!
<h1>This is a test & hopefully it works!</h1>

**********************************
Here is the code:
**********************************

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.HttpUtility" %>
<html>
<head>
<title>HTMLEncode and URLEncode the ASP.NET Way</title>

<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)

''Assign our test url to a variable, assign the variable to our
label control
Dim strURLText as String = "urlencode.asp?test=1&test1=2"
strURL.Text = strURLText

''Encode the test url, assign it to a variable, assign the
variable to our label control
Dim strURLEncodeText As String = UrlEncode(strURLText)
strURLEncode.Text = strURLEncodeText

''Assign our test url to a variable, assign the variable to our
label control
Dim strHTMLText As String = "<h1>This is a test & hopefully it
works!</h1>"
strHTML.Text = strHTMLText

''Encode the test html, assign it to a variable, assign the
variable to our label control
Dim strHTMLEncodeText As String = HtmlEncode(strHTMLText)
strHTMLEncode.Text = strHTMLEncodeText

End Sub
</script>
</head>
<body>
<asp:label id="strURL" forecolor="red" font-bold="true" runat=server/>
<p>
<asp:label id="strURLEncode" forecolor="blue" font-bold="true" runat=server/>
<p>
<asp:label id="strHTML" forecolor="red" font-bold="true" runat=server/>
<p>
<asp:label id="strHTMLEncode" forecolor="blue" font-bold="true" runat=server/>
</body>
</html>

其它资源
来源声明

版权与免责声明
1、本站所发布的文章仅供技术交流参考,本站不主张将其做为决策的依据,浏览者可自愿选择采信与否,本站不对因采信这些信息所产生的任何问题负责。
2、本站部分文章来源于网络,其版权为原权利人所有。由于来源之故,有的文章未能获得作者姓名,署“未知”或“佚名”。对于这些文章,有知悉作者姓名的请告知本站,以便及时署名。如果作者要求删除,我们将予以删除。除此之外本站不再承担其它责任。
3、本站部分文章来源于本站原创,本站拥有所有权利。
4、如对本站发布的信息有异议,请联系我们,经本站确认后,将在三个工作日内做出修改或删除处理。
请参阅权责声明