ASP.Net/Server/URL Encode Decode
Содержание
Decoding Encoded HTML Strings (VB.net)
<source lang="csharp">
<%@ 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>
</source>
Encoding HTML Strings (VB.net)
<source lang="csharp">
<%@ 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>
</source>
Encoding URL Strings in C#
<source lang="csharp">
<%@ 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>
</source>
Encoding URL Strings in VB.net
<source lang="csharp">
<%@ 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>
</source>
Encoding URL Strings (VB.net)
<source lang="csharp">
<%@ 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>
--%>
</source>
Server.HtmlEncode (VB.net)
<source lang="csharp">
<%@ Page Language="vb" %> <html>
<head> <title>Server property example</title> <script runat="server"> Sub Page_Load() Message.Text = Server.HtmlEncode("Hello, World!") End Sub </script> </head>
<body>
<asp:label id="Message" runat="server"/>
</body> </html>
</source>
URL Encode and Decode Demo (VB.net)
<source lang="csharp">
<%@ 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) & "
" _ & "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) & "
" _ & "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) & "
" _ & "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) & "
" _ & "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"> <asp:Label
id="lblMessage1" runat="server" Text="Enter HTML text to encode" Font-Bold="True"
/> <asp:TextBox
id="txtHTMLEncode" runat="server"
/>
<asp:button
id="butEncode" runat="server" text="Encode" Type="Submit" OnClick="SubmitHTMLEncode_Click"
/>
<asp:Label
id="lblMessage2" runat="server" Text="Enter HTML text to decode" Font-Bold="True"
/> <asp:TextBox
id="txtHTMLDecode" runat="server"
/>
<asp:button
id="butDecode" runat="server" text="Decode" Type="Submit" OnClick="SubmitHTMLDecode_Click"
/>
<asp:Label
id="lblMessage3" runat="server" Text="Enter URL text to encode" Font-Bold="True"
/> <asp:TextBox
id="txtURLEncode" runat="server"
/>
<asp:button
id="butEncode2" runat="server" text="Encode" Type="Submit" OnClick="SubmitURLEncode_Click"
/>
<asp:Label
id="lblMessage4" runat="server" Text="Enter URL text to decode" Font-Bold="True"
/> <asp:TextBox
id="txtURLDecode" runat="server"
/>
<asp:button
id="butDecode2" runat="server" text="Decode" Type="Submit" OnClick="SubmitURLDecode_Click"
/> </Form> </BODY> </HTML>
</source>