0

I am converting a VB6 project to C#. In the VB6 project, we have manually created methods for url building. In C#, we have Uri, UriBuilder etc. libraries.

I need to find a way to build the url by removing the additional backward, forward slashes.

For example, if I use UriBuilder,

UriBuilder uriBuilder = new UriBuilder();
uriBuilder.Scheme = "https";
uriBuilder.Host = @"www.facebook.com/";
uriBuilder.Path = @"/asa/dsd\";

When I call uriBuilder.ToString(), here I got the result as

https://www.facebook.com//asa/dsd/

From the output, UriBuilder is not removing the additional forward/ backward slashes in the host name.

Could anybody please let me know, is there a library in C# which we can use to build the url by removing the additional slashes?

Thank you.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
ApsSanj
  • 549
  • 7
  • 23
  • 2
    Possible duplicate of [Using System.Uri to remove redundant slash](https://stackoverflow.com/questions/19689778/using-system-uri-to-remove-redundant-slash) – styx Aug 28 '18 at 05:44

2 Answers2

3

try this:

var baseUri = new Uri("https://www.facebook.com/");
var yourUri = new Uri(baseUri, @"/asa/dsd\");
var result = yourUri.ToString(); // https://www.facebook.com/asa/dsd/ 
vhr
  • 1,528
  • 1
  • 13
  • 21
1

You could use string.Trim method to get rid of unwanted slashes at the beginning and at the start of your string:

UriBuilder uriBuilder = new UriBuilder();
uriBuilder.Scheme = "https";
uriBuilder.Host = @"www.facebook.com/";
uriBuilder.Host = uriBuilder.Host.Trim('/');
uriBuilder.Path = @"/asa/dsd\";
uriBuilder.Path = uriBuilder.Path.Trim('/');
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69