Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Uri.ToString() Method in C#
The Uri.ToString() method in C# is used to get a canonical string representation for the specified Uri instance. This method returns the complete URI as a formatted string, including all components such as scheme, host, path, query, and fragment.
Syntax
Following is the syntax −
public override string ToString();
Return Value
This method returns a string that contains the unescaped canonical representation of the Uri instance. All characters are unescaped except #, ?, and %.
Using Uri.ToString() Method
Basic Example
Let us see how to use the Uri.ToString() method to get string representations of different URI formats −
using System;
public class Demo {
public static void Main() {
Uri newURI1 = new Uri("https://www.tutorialspoint.com/");
Console.WriteLine("URI = " + newURI1);
Console.WriteLine("String representation = " + newURI1.ToString());
Uri newURI2 = new Uri("https://www.tutorialspoint.com/jquery.htm#abcd");
Console.WriteLine("\nURI = " + newURI2);
Console.WriteLine("String representation = " + newURI2.ToString());
if(newURI1.Equals(newURI2))
Console.WriteLine("\nBoth the URIs are equal!");
else
Console.WriteLine("\nBoth the URIs aren't equal!");
Uri res = newURI1.MakeRelativeUri(newURI2);
Console.WriteLine("Relative uri = " + res);
}
}
The output of the above code is −
URI = https://www.tutorialspoint.com/ String representation = https://www.tutorialspoint.com/ URI = https://www.tutorialspoint.com/jquery.htm#abcd String representation = https://www.tutorialspoint.com/jquery.htm#abcd Both the URIs aren't equal! Relative uri = jquery.htm#abcd
Uri Components in ToString() Output
Example
The following example demonstrates how ToString() preserves all URI components −
using System;
public class UriComponentsDemo {
public static void Main() {
Uri complexUri = new Uri("https://user:pass@www.example.com:8080/path/to/page?query=value¶m=test#section");
Console.WriteLine("Complete URI: " + complexUri.ToString());
Console.WriteLine("Scheme: " + complexUri.Scheme);
Console.WriteLine("Host: " + complexUri.Host);
Console.WriteLine("Port: " + complexUri.Port);
Console.WriteLine("Path: " + complexUri.AbsolutePath);
Console.WriteLine("Query: " + complexUri.Query);
Console.WriteLine("Fragment: " + complexUri.Fragment);
}
}
The output of the above code is −
Complete URI: https://user:pass@www.example.com:8080/path/to/page?query=value¶m=test#section Scheme: https Host: www.example.com Port: 8080 Path: /path/to/page Query: ?query=value¶m=test Fragment: #section
ToString() vs Other Uri Methods
| Method | Description | Example Output |
|---|---|---|
ToString() |
Returns complete unescaped URI | https://example.com/path?query=value#fragment |
AbsoluteUri |
Returns complete escaped URI | https://example.com/path?query=value#fragment |
OriginalString |
Returns URI as originally passed to constructor | Preserves original formatting |
Example
using System;
public class UriMethodsComparison {
public static void Main() {
Uri uri = new Uri("https://example.com/path with spaces?query=hello world");
Console.WriteLine("ToString(): " + uri.ToString());
Console.WriteLine("AbsoluteUri: " + uri.AbsoluteUri);
Console.WriteLine("OriginalString: " + uri.OriginalString);
}
}
The output of the above code is −
ToString(): https://example.com/path with spaces?query=hello world AbsoluteUri: https://example.com/path%20with%20spaces?query=hello%20world OriginalString: https://example.com/path with spaces?query=hello world
Conclusion
The Uri.ToString() method provides a convenient way to get the canonical string representation of a URI instance with unescaped characters. It preserves all URI components and is useful for display purposes and string comparisons where human-readable format is preferred over escaped format.
