tags:

views:

611

answers:

4

I'm looking it over and it seems to be that it is fundamentally broken.

  • Only 5 instance methods aren't marked obsolete.
  • There doesn't appear to be any built-in way to parse query-string variables.
  • There are no methods to mutate the Uri, for example appending a new query variable.
  • HttpUtility works on strings, not URIs

So is there anything it is good for? Should I really be using this instead of just strings?

+1  A: 

UriBuilder is for mutation. HttpUtility is also good to know about (e.g. for query string parsing).

http://msdn.microsoft.com/en-us/library/system.uribuilder.aspx

http://msdn.microsoft.com/en-us/library/system.web.httputility.parsequerystring.aspx

Brian
I'm well aware of HttpUtility, but it works on strings not URIs.
Jonathan Allen
But that's the point. You can use it with Uri.Query to parse the query string.
Richard Szalay
+5  A: 

I wouldn't say the Uri class is fundamentally broken at all. The purpose of the Uri class is to provide a compact and standard representation of a URI. The Uri class encapsulates all of the logic needed to return the URI in a canonical form and to provide support for IPV4 and IPV6 notations, and IRI support.

The Uri class is not designed to allow changing the Uri once it has been created; if you want that level of mutability you should use a UriBuilder instead.

The benefit to using Uri (or UriBuilder) over strings is that you get a lot of validation built in to ensure that the given address is well-formed, capabilities to make relative URIs from absolute ones, etc. Essentially, you can think of a Uri as an actual data type, so using one provides a level of strong typing.

Scott Dorman
A string also provides a "compact and standard representation of a URI". And the only way to truly validate a URI is to call the resource in question, as each resource tends to have its own rules.
Jonathan Allen
If you're talking about validating the resource exists, yes, but the Uri class will validate that the address is well-formed, which is different. A string doesn't allow you to easily create relative URIs, easily determine if the given URI is absolute or not, etc.
Scott Dorman
also a Uri will canonicalise the URI which can be very important in some applications
RobV
+3  A: 

It depends on what you are doing. I can think of at least one scenario where I would use it, and that is to turn relative URLs into absolutes. Say, for example, you have some HTML you've pulled form somewhere but you don't necessarily know if the URL is relative or absolute. You could write your own parsing logic, or you could use the Uri class like:

Uri link = new Uri(new Uri(webSiteAddress), linkPulledFromSite);
string absoluteUrl = link.AbsoluteUri;

Then you would easily have the absolute URL without the worry. It all just depends on what you are doing :-)

Jason Whitehorn
Ok, that's a nifty trick. But it doesn't really justify the Uri class as a whole.
Jonathan Allen
A: 

Why do all of the file classes and web classes take unvalidated strings? The .NET Framework designers really dropped the ball on string encapsulation classes like this.

If you are concerned about the verification of your string data, almost all of your strings should go into some kind of container. Without one it's really difficult to determine the state of your string. How is it currently formatted? Does it contain invalid characters? Is it alright to launch it right now?

Rick Minerich