ASP.Net/Server/URL Encode Decode
Версия от 15:30, 26 мая 2010; (обсуждение)
Содержание
Decoding Encoded HTML Strings (VB.net)
<%@ Page Language="VB" %>
<html>
<head>
<title>Decoding Encoded HTML Strings</title>
<script runat="server">
Sub HtmlDecode()
Dim StrToDecode As String
Dim StrToReturn As String
StrToDecode = "<p>Hello, World!</p>"
StrToReturn = Server.HtmlDecode(StrToDecode)
Response.Write(StrToReturn)
End Sub
</script>
</head>
<body>
<% HtmlDecode %>
</body>
</html>
Encoding HTML Strings (VB.net)
<%@ Page Language="VB" %>
<html>
<head>
<title>Encoding HTML Strings</title>
<script runat="server">
Sub HtmlEncode()
Dim StrToEncode As String
Dim StrToReturn As String
StrToEncode = "<%@ Page Language=""VB"" %>"
StrToReturn = Server.HtmlEncode(StrToEncode)
Response.Write(StrToReturn)
End Sub
</script>
</head>
<body>
<% HtmlEncode %>
</body>
</html>
Encoding URL Strings in C#
<%@ Page Language="C#" %>
<html>
<head>
<title>Encoding URL Strings</title>
<script runat="server">
void UrlPathEncode()
{
string StrToEncode;
string StrToReturn;
StrToEncode = "UrlPathEncode.aspx? Arg = foo";
StrToReturn = Server.UrlPathEncode(StrToEncode);
Response.Write("<a href=\"" + StrToReturn + "\">" + StrToReturn + "</a>");
}
</script>
</head>
<body>
<% UrlPathEncode(); %>
</body>
</html>
Encoding URL Strings in VB.net
<%@ Page Language="VB" %>
<html>
<head>
<title>Encoding URL Strings</title>
<script runat="server">
Sub UrlPathEncode()
Dim StrToEncode As String
Dim StrToReturn As String
StrToEncode = "UrlPathEncode.aspx? Arg = foo"
StrToReturn = Server.UrlPathEncode(StrToEncode)
Response.Write("<a href=""" & StrToReturn & """>" & StrToReturn & "</a>")
End Sub
</script>
</head>
<body>
<% UrlPathEncode %>
</body>
</html>
Encoding URL Strings (VB.net)
<%@ Page Language="VB" %>
<html>
<head>
<title>Encoding URL Strings</title>
<script runat="server">
Sub UrlEncode()
Dim StrToEncode As String
Dim StrToReturn As String
StrToEncode = "Hello, World!"
StrToReturn = Server.UrlEncode(StrToEncode)
Response.Write("<a href=""UrlDecode.aspx?StrToDecode=" & _
StrToReturn & """>" & StrToReturn & " - Click to Decode!</a>")
End Sub
</script>
</head>
<body>
<% UrlEncode %>
</body>
</html>
<%-- UrlDecode.aspx
<%@ Page Language="VB" %>
<html>
<head>
<title>Decoding Encoded URL Strings</title>
<script runat="server">
Sub UrlDecode()
Dim StrToDecode As String
Dim StrToReturn As String
StrToDecode = Request.QueryString("StrToDecode")
StrToReturn = Server.UrlDecode(StrToDecode)
Response.Write(StrToReturn)
End Sub
</script>
</head>
<body>
<% UrlDecode %>
</body>
</html>
--%>
Server.HtmlEncode (VB.net)
<%@ Page Language="vb" %>
<html>
<head>
<title>Server property example</title>
<script runat="server">
Sub Page_Load()
Message.Text = Server.HtmlEncode("<em>Hello, World!</em>")
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
URL Encode and Decode Demo (VB.net)
<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub SubmitHTMLEncode_Click(Sender As Object, E As EventArgs)
Dim MySU as HTTPServerUtility
MySU = Server
lblMessage1.Text = "Encoded: " _
& MySU.HTMLEnCode(txtHTMLEncode.Text) & "<BR>" _
& "Enter HTML text to encode"
End Sub
Sub SubmitHTMLDecode_Click(Sender As Object, E As EventArgs)
Dim MySU as HTTPServerUtility
MySU = Server
lblMessage2.Text = "Decoded: " _
& MySU.HTMLDeCode(txtHTMLDecode.Text) & "<BR>" _
& "Enter HTML text to decode"
End Sub
Sub SubmitURLEncode_Click(Sender As Object, E As EventArgs)
Dim MySU as HTTPServerUtility
MySU = Server
lblMessage3.Text = "Encoded: " _
& MySU.URLEnCode(txtURLEncode.Text) & "<BR>" _
& "Enter URL text to encode"
End Sub
Sub SubmitURLDecode_Click(Sender As Object, E As EventArgs)
Dim MySU as HTTPServerUtility
MySU = Server
lblMessage4.Text = "Decoded: " _
& MySU.URLDeCode(txtURLDecode.Text) & "<BR>" _
& "Enter URL text to decode"
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Encoding and DeCoding Sample Page</TITLE>
</HEAD>
<BODY TEXT="black" LINK="darkred" VLINK="darkred" ALINK="red" LEFTMARGIN="40" TOPMARGIN="40">
<form runat="server">
<Font Face="Tahoma">
<asp:Label
id="lblMessage1"
runat="server"
Text="Enter HTML text to encode"
Font-Bold="True"
/>
<asp:TextBox
id="txtHTMLEncode"
runat="server"
/>
<BR><BR>
<asp:button
id="butEncode"
runat="server"
text="Encode"
Type="Submit"
OnClick="SubmitHTMLEncode_Click"
/>
<BR><BR>
<asp:Label
id="lblMessage2"
runat="server"
Text="Enter HTML text to decode"
Font-Bold="True"
/>
<asp:TextBox
id="txtHTMLDecode"
runat="server"
/>
<BR><BR>
<asp:button
id="butDecode"
runat="server"
text="Decode"
Type="Submit"
OnClick="SubmitHTMLDecode_Click"
/>
<BR><BR>
<asp:Label
id="lblMessage3"
runat="server"
Text="Enter URL text to encode"
Font-Bold="True"
/>
<asp:TextBox
id="txtURLEncode"
runat="server"
/>
<BR><BR>
<asp:button
id="butEncode2"
runat="server"
text="Encode"
Type="Submit"
OnClick="SubmitURLEncode_Click"
/>
<BR><BR>
<asp:Label
id="lblMessage4"
runat="server"
Text="Enter URL text to decode"
Font-Bold="True"
/>
<asp:TextBox
id="txtURLDecode"
runat="server"
/>
<BR><BR>
<asp:button
id="butDecode2"
runat="server"
text="Decode"
Type="Submit"
OnClick="SubmitURLDecode_Click"
/>
</Font>
</Form>
</BODY>
</HTML>