Changing the port of the System.Uri in C#

Changing the port of the System.Uri in C#

In C#, the System.Uri class represents a Uniform Resource Identifier, which includes a combination of components such as scheme, host, port, path, query, and fragment. The Uri class is designed to be immutable, which means you cannot directly change its properties once it's created.

However, if you need to modify the port of a Uri, you can create a new Uri instance with the updated port value. Here's how you can do it:

using System; class Program { static void Main() { // Original Uri string originalUriString = "http://example.com:8080/path"; Uri originalUri = new Uri(originalUriString); // New port value int newPort = 9090; // Create a new Uri with the updated port Uri updatedUri = ChangePort(originalUri, newPort); // Display the updated Uri Console.WriteLine("Original Uri: " + originalUri.ToString()); Console.WriteLine("Updated Uri: " + updatedUri.ToString()); } static Uri ChangePort(Uri originalUri, int newPort) { // Create a new UriBuilder based on the original Uri UriBuilder uriBuilder = new UriBuilder(originalUri); // Update the port with the new value uriBuilder.Port = newPort; // Return the updated Uri return uriBuilder.Uri; } } 

In the code above, the ChangePort method creates a new UriBuilder based on the original Uri, then updates the Port property of the UriBuilder with the desired new port value. Finally, it returns the updated Uri by calling uriBuilder.Uri.

Keep in mind that while you can change the port in this manner, other components of the Uri are still immutable. If you need to modify other parts of the Uri, you'll need to follow a similar approach by creating a new UriBuilder and updating its properties accordingly.

Examples

  1. "C# change Uri port number"

    • Code:
      // Original Uri Uri originalUri = new Uri("http://example.com:8080/path"); // Change port number Uri modifiedUri = new UriBuilder(originalUri) { Port = 9090 }.Uri; 
    • Description: Use the UriBuilder to modify the port number of a Uri.
  2. "C# update Uri with a different port"

    • Code:
      // Original Uri Uri originalUri = new Uri("http://example.com:8080/path"); // Update port number Uri updatedUri = new UriBuilder(originalUri) { Port = 9090 }.Uri; 
    • Description: Employ the UriBuilder to update the port number in a Uri.
  3. "C# change Uri port dynamically"

    • Code:
      // Original Uri Uri originalUri = new Uri("http://example.com:8080/path"); // Change port dynamically int newPort = 9090; Uri modifiedUri = new UriBuilder(originalUri) { Port = newPort }.Uri; 
    • Description: Change the port number dynamically based on a variable.
  4. "C# modify Uri port with string concatenation"

    • Code:
      // Original Uri Uri originalUri = new Uri("http://example.com:8080/path"); // Modify port with string concatenation string newUriString = originalUri.ToString().Replace(":8080", ":9090"); Uri modifiedUri = new Uri(newUriString); 
    • Description: Use string concatenation to modify the port in a Uri string representation.
  5. "C# change Uri port in a query string"

    • Code:
      // Original Uri Uri originalUri = new Uri("http://example.com:8080/path?param=value"); // Change port number without affecting query string Uri modifiedUri = new UriBuilder(originalUri) { Port = 9090, Query = originalUri.Query }.Uri; 
    • Description: Modify the port number without affecting the query string in a Uri.
  6. "C# set Uri port to default"

    • Code:
      // Original Uri Uri originalUri = new Uri("http://example.com:8080/path"); // Set port to default (80 for HTTP) Uri defaultPortUri = new UriBuilder(originalUri) { Port = -1 }.Uri; 
    • Description: Set the port to its default value (80 for HTTP) using -1 in the UriBuilder.
  7. "C# change Uri port with regex"

    • Code:
      // Original Uri Uri originalUri = new Uri("http://example.com:8080/path"); // Change port using regex string modifiedUriString = Regex.Replace(originalUri.ToString(), ":\\d+", ":9090"); Uri modifiedUri = new Uri(modifiedUriString); 
    • Description: Use regex to change the port number in a Uri string representation.
  8. "C# modify Uri port with UriComponents"

    • Code:
      // Original Uri Uri originalUri = new Uri("http://example.com:8080/path"); // Modify port using UriComponents UriBuilder uriBuilder = new UriBuilder(originalUri); uriBuilder.Port = 9090; Uri modifiedUri = uriBuilder.Uri; 
    • Description: Modify the port using the UriComponents of the UriBuilder.
  9. "C# change Uri port with a different scheme"

    • Code:
      // Original Uri Uri originalUri = new Uri("http://example.com:8080/path"); // Change port with a different scheme Uri modifiedUri = new UriBuilder(originalUri) { Scheme = "https", Port = 9090 }.Uri; 
    • Description: Change both the scheme and port number in a Uri.
  10. "C# update Uri port with a UriTemplate"

    • Code:
      // Original Uri Uri originalUri = new Uri("http://example.com:8080/path"); // Update port using a UriTemplate UriTemplate template = new UriTemplate("{scheme}://{host}:{port}/{path}"); Uri modifiedUri = template.BindByPosition(new UriTemplateMatch(originalUri), "http", "example.com", "9090", "path"); 
    • Description: Use a UriTemplate to update the port while preserving other components of the Uri.

More Tags

nagios varbinary activity-indicator sandbox bluetooth android-softkeyboard discord-jda absolute-path solid-principles vb6

More C# Questions

More Statistics Calculators

More Investment Calculators

More Physical chemistry Calculators

More Trees & Forestry Calculators