0

I've just started using .netcore.

I need to create a url, I've come across UriBuilder however I can only see example of using that with named paramters e.g.: localhost?value=abc&another=def, however I want to construct without parameter names, so:

localhost/abc/def/

Is this possible, I was thinking maybe something like the below:

var request = new HttpRequestMessage();
request.RequestUri = new Uri(_myUrl);
request.Method = HttpMethod.Get;

var uriBuilder = new UriBuilder(request.RequestUri);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query[0] = "abc"

Any help appreciated.

userMod2
  • 8,312
  • 13
  • 63
  • 115

1 Answers1

2

That's not how UriBuilder or URIs work. What you are trying to change/set is the path of the URI. That is done using the Path property, or using the relevant constructor of UriBuilder. Your example shows how to change/set the Query-String (key-value pairs, basically).

How you create the path is not the business of the UriBuilder class (see @Irdis comment about an option).

Basically, you can create your path manually using string manipulation of your choice, then assign it to the Path property, e.g.:

var b = new UriBuilder(...);
// Set other properties to your requirements
b.Path = "/the/path/ optional with spaces and other stuff/";

You can then use the UriBuilder.Uri property to get the full URI or use the UriBuilder.ToString() method to get the properly escaped URI. For the above example that would be something like:

http://localhost/the/path/%20optional%20with%20spaces%20and%20other%20stuff/
Christian.K
  • 47,778
  • 10
  • 99
  • 143