Recipe14.14.Using the UriBuilder Class


Recipe 14.14. Using the UriBuilder Class

Problem

You want to avoid making URI syntax errors when creating a URI.

Solution

Use the UriBuilder class to add each piece without worrying about syntax or placement in the string.

Building a URI programmatically can be challenging to do correctly in all instances. Using the UriBuilder can help to simplify it. For instance, if you needed to assemble an HTTP Uri that looked like this:

http://user:password@localhost:8080/www.abc.com/homepagehtm?item=1233;html=<h1>Heading</h1>#stuff

you would need to understand the layout of the HTTP Uri, which is this:

  [scheme]://[user]:[password]@[host/authority]:[port]/[path];[params]?[query string]#[fragment] 

It is very possible that information could come in that has only some of these pieces, or all of the pieces might be present. The UriBuilder allows the code to set properties for each of the components of the URI. This is great except for one small glitch. Every time you set the Query property, the UriBuilder class appends a ? to the front of the query string information. This means if code is written in this manner:

 UriBuilder ub = new UriBuilder(); ub.Query = "item=1233"; ub.Query += "html-<h1>heading</h1>"; 

the resulting query string looks like this with two question marks:

 ??item=1233;html=<h1>heading</h1> 

To correct this sad state of affairs, use the UriBuilderFix which overloads the Query property and deals with this in a more reasonable manner. UriBuilderFix is a light wrapper for UriBuilder that cleans up the Query property behavior.

 public class UriBuilderFix : UriBuilder {     public UriBuilderFix() : base( )     {     }     public new string Query     {         get         {             return base.Query;         }          set          {           if (!string.IsNullOrEmpty(value))            {                 if (value[0] == '?')                     // Trim off the leading ? as the underlying                     // UriBuilder class will add one to the                     // query string. Also prepend ; for additional items.                     base.Query = value.Substring(1);                 else                     base.Query = value;             }             else                 base.Query = string.Empty;         }     } } 

The UriBuilderFix is used just like the UriBuilder, except you now get the expected output from adding to the query string:

 UriBuilderFix ubf = new  UriBuilderFix(); ubf.Scheme = "http"; ubf.UserName = "user"; ubf.Password = "password"; ubf.Host = "localhost"; ubf.Port = 8080; ubf.Path = "www.abc.com/home page.htm"; //The Query property contains any query information included in the Uri. //Query information is separated from the path information by a question mark (?) and // //continues to the end of the Uri. The query information returned includes the ///// //leading question mark. //The query information is escaped according to RFC 2396. //Setting the Query property to null or to System.String.Empty clears the property. //Note: Do not append a string directly to this property. //Instead retrieve the property value as a string, remove the leading question mark, //append the new query string, and set the property with the combined string. ubf.Query = "item=1233"; ubf.Query += ";html=<h1>heading</h1>"; ubf.Fragment = "stuff"; Console.WriteLine("Absolute Composed Uri: " + ubf.Uri.AbsoluteUri); Console.WriteLine("Composed Uri: " + ubf.ToString()); 

This example produces the following output:

 Absolute Composed Uri: http://user:password@localhost:8080/www.abc.com/home%20page. htm?item=1233;html=%3Ch1%3Eheading%3C/h1%3E Composed Uri: http://user:password@localhost:8080/www.abc.com/home%20page.htm?item=1233;html= %3Ch1%3Eheading%3C/h1%3E 

Discussion

Even without the addition of the Query behavior in BetterUriBuilder, UriBuilder is a great way to build up Uris without resorting to assembling the whole string yourself. Once the construction of the Uri is complete, get the Uri object from the UriBuilder. Uri property to use it.

See Also

See Recipes 14.3 and 14.4; see the "UriBuilder Class" and "Uri Class" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net