ASP.NET 中的 HTML 编码和 URL 编码

  server.htmlencodeandserver.urlencodeareverycommonfunctionsusedbyaspdevelopers.well,asyoumayhaveguessed,thesetwofunctionshaveasp.netcounterparts.htmlencodeandurlencode(noticecase)arepartofthesystem.web.httputilitynamespace.
 
  hereisthedemo
 
  urlencode.asp?test=1&test1=2
 
  urlencode.asp%3ftest%3d1%26test1%3d2
 
  thisisatest&hopefullyitworks!
 
  <h1>thisisatest&hopefullyitworks!</h1>
 
  **********************************
 
  hereisthecode:
 
  **********************************
 
  <%@pagelanguage="vb"%>
 
  <%@importnamespace="system.web.httputility"%>
 
  <html>
 
  <head>
 
  <title>htmlencodeandurlencodetheasp.netway</title>
 
  <scriptlanguage="vb"runat="server">
 
  subpage_load(senderasobject,easeventargs)
 
  assignourtesturltoavariable,assignthevariabletoour
 
  labelcontrol
 
  dimstrurltextasstring="urlencode.asp?test=1&test1=2"
 
  strurl.text=strurltext
 
  encodethetesturl,assignittoavariable,assignthe
 
  variabletoourlabelcontrol
 
  dimstrurlencodetextasstring=urlencode(strurltext)
 
  strurlencode.text=strurlencodetext
 
  assignourtesturltoavariable,assignthevariabletoour
 
  labelcontrol
 
  dimstrhtmltextasstring="<h1>thisisatest&hopefullyit
 
  works!</h1>"
 
  strhtml.text=strhtmltext
 
  encodethetesthtml,assignittoavariable,assignthe
 
  variabletoourlabelcontrol
 
  dimstrhtmlencodetextasstring=htmlencode(strhtmltext)
 
  strhtmlencode.text=strhtmlencodetext
 
  endsub
 
  </script>
 
  </head>
 
  <body>
 
  <asp:labelid="strurl"forecolor="red"font-bold="true"runat=server/>
 
  <p>
 
  <asp:labelid="strurlencode"forecolor="blue"font-bold="true"runat=server/>
 
  <p>
 
  <asp:labelid="strhtml"forecolor="red"font-bold="true"runat=server/>
 
  <p>
 
  <asp:labelid="strhtmlencode"forecolor="blue"font-bold="true"runat=server/>