2005-04-21

A quickie: UriBuilder

Here's a quick note regarding the Uri class I just mentioned previously. A string can be turned into a Uri, and this action will split the information in the string into several pieces: a hostname, a query, a protocol. If you want to make a change to one of these pieces, you are not required to modify the string and then use it to create a new Uri object.

Instead, use the UriBuilder class.

With the UriBuilder class, you can basically hand roll your own Uris without having to make a new Uri from scratch each time. (You can easily see how this can save you some time writing, say, spiders. A nice feature for a spider to have is to be able to hit a single host over multiple protocols should one protocol ever fail. Instead of checking the host string for "http://", removing the first two characters, adding an 'f' at the front, and then calling "new Uri()" again, you can just go into your UriBuilder object and change the Scheme property from "HTTP" to "FTP". It's less work, probably a lot faster, and much less likely to introduce parsing bugs into your application.)

No comments: