HttpUtility Class with ASP.NET 

Everybody knows how to use the ASP.NET Server object. This object exists in different versions since Microsoft created Classic ASP.

You can use the ASP.NET Server object to decode/encode HTML, so it will be readable in your favorite browser. Or you can decode/encode URL strings for a reliable HTTP transmission between the webserver and the client. Nothing new I guess.

The ASP.NET Server object has methods and properties exposed by the HttpServerUtility class. The HttpServerUtility class is using the HttpUtility class internally.

If we take a look at the HttpUtility class, we can see different methods. Some of these methods can be very useful in some cases.
So before you start coding yourself some really nice code, please take a look at what the .NET framework is providing you for free. In that way, you can spend more time doing fun stuff! ;-)

HttpUtility.HtmlAttributeEncode - Minimally converts a string to an HTML-encoded string. You can use this for example with dropdownlists. (don’t replace strange characters on a itembound e.g.)

HttpUtility.ParseQueryString - Parses a query string into a NameValueCollection UTF8 encoding.

Example HttpUtility.ParseQueryString (C#)

    1 <%@ Page Language="C#"%>
    2 
    3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    4     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    5 <script runat="server">
    6 
    7   protected void Page_Load(object sender, EventArgs e)
    8   {
    9     String currurl = HttpContext.Current.Request.RawUrl;
   10     String querystring = null ;
   11 
   12     // Check to make sure some query string variables
   13     // exist and if not add some and redirect.
   14     int iqs = currurl.IndexOf('?');
   15     if (iqs == -1)
   16     {
   17       String redirecturl = currurl + "?var1=1&var2=2+2%2f3&var1=3";
   18       Response.Redirect(redirecturl, true); 
   19     }
   20     // If query string variables exist, put them in
   21     // a string.
   22     else if (iqs >= 0)
   23     {
   24       querystring = (iqs < currurl.Length - 1) ? 
   25             currurl.Substring(iqs + 1) : String.Empty;
   26     }
   27 
   28     // Parse the query string variables into a NameValueCollection.
   29     NameValueCollection qscoll = HttpUtility.ParseQueryString(querystring);
   30 
   31     // Iterate through the collection.
   32     StringBuilder sb = new StringBuilder("<br />");
   33     foreach (String s in qscoll.AllKeys)
   34     {
   35       sb.Append(s + " - " + qscoll[s] + "<br />");
   36     }
   37 
   38     // Write the result to a label.
   39     ParseOutput.Text = sb.ToString();
   40 
   41   }
   42 </script>
   43 
   44 <html  >
   45 <head id="Head1" runat="server">
   46     <title>HttpUtility ParseQueryString Example</title>
   47 </head>
   48 <body>
   49     <form id="form1" runat="server">
   50       Query string variables are:
   51       <asp:Label  id="ParseOutput"
   52                   runat="server" />
   53     </form>
   54 </body>
   55 </html>
Posted on 28-04-2009 by Tim van der Weijde
0 Comments  |  Trackback Url  |  Link to this post
Tags:

Links to this post

Comments

Name:
URL:
Email:
Comments:

CAPTCHA Image Validation