Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any reason to use System.Uri?

Tags:

.net

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?

like image 740
Jonathan Allen Avatar asked Oct 19 '08 00:10

Jonathan Allen


People also ask

What is a system URI?

A URI is a compact representation of a resource available to your application on the intranet or internet. The Uri class defines the properties and methods for handling URIs, including parsing, comparing, and combining. The Uri class properties are read-only; to create a modifiable object, use the UriBuilder class.

What is URI string in C#?

Uri(String, UriKind) Initializes a new instance of the Uri class with the specified URI. This constructor allows you to specify if the URI string is a relative URI, absolute URI, or is indeterminate. Uri(Uri, String) Initializes a new instance of the Uri class based on the specified base URI and relative URI string.

What is string URI?

A URI is short for Uniform Resource Identifier. This URI is a string that contains 1. The URL you are pointing towards, 2. The information you are trying to passover.

What is a URI object?

A Uniform Resource Identifier (URI) is a unique sequence of characters that identifies a logical or physical resource used by web technologies. URIs may be used to identify anything, including real-world objects, such as people and places, concepts, or information resources such as web pages and books.


1 Answers

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.

like image 127
Scott Dorman Avatar answered Oct 05 '22 21:10

Scott Dorman