8

OK, so having digested such excellent answers as this one difference between URLs and URIs, I think I now understand the distinction between the two.

What I now don't understand is why the .NET Framework has a UriBuilder class, which - as far as I can tell - only works on URIs which are resource locators and should therefore properly be called a UrlBuilder.

Can someone give me an example of UriBuilder being used to build a URI which is not a resource locator? Or some rationale for this decision in the design of the .NET framework?

Community
  • 1
  • 1
Dylan Beattie
  • 50,029
  • 31
  • 120
  • 189

2 Answers2

14

When you think about it, a UriBuilder is more powerful and broader than a URL Builder. Since every URL is a URI, a UriBuilder is inherently a URL Builder.

So basically the question is when could you use a UriBuilder for a non-URL URI. Well, I guess the best example would be for a URN (since URLs and URNs are basically locators and names -- the two classifications of URIs). A URN could be used, for example, to build a uuid (i.e. urn:uuid:6e8bc430-9c3a-11d9-9669-0800200c9a66) or an ISBN (i.e. ISBN 0-486-27557-4). These URNs don't contain location information that points to a resource like URLs do, they just contain names or ids. A UriBuilder would be able to build not just URLs (which point to a resource), but it would also be able to build URNs. If you look at the Uri.Scheme property, you will see uuid in the list of possible schemes for a Uri.

Also, .NET does technically have a UrlBuilder class.

Michael Frederick
  • 17,057
  • 3
  • 41
  • 57
  • The fact that this question is now the #1 Google result for "UriBuilder UUID" suggests that whilst this usage is *technically* supported, it's of somewhat limited practical application... :) – Dylan Beattie Jul 17 '12 at 15:01
  • @Dylan the fact that it isn't used for that is not relevant. Relevant is what the proper name for the class was when it was designed. Since it does support more than building of Urls calling it UrlBuilder would have lead to an interesting SO question of why somebody called it UrlBuilder when you could build UUID with it as well :) – Eddy Jul 18 '12 at 09:59
0
Uri _uri = new Uri(URL);
            string tURL = _uri.Scheme + "://" + _uri.Host + _uri.LocalPath;
            Console.WriteLine("Before: " + tURL);
            uri = new UriBuilder(tURL);
            Console.WriteLine("After:  " + uri.Uri.ToString());
Frank R.
  • 2,206
  • 4
  • 29
  • 69