Adding Multiple Segments Using System.Uri

var baseUri = new Uri("http://localhost/"); var uri1 = new Uri(baseUri, "1"); var uri2 = new Uri(uri1, "2"); 

Unexpectedly uri2 http: // localhost / 2 . How can I add to uri1 so that it http: // localhost / 1/2 starts? Does this do Uri or do I need to step back to the lines? By the way, I almost always tried to add leading / trailing slashes.

+6
source share
1 answer

"1" and "2" are the "file name part" of the URL. If you make "1" more like a directory path, it will work fine "1 /":

 var baseUri = new Uri("http://localhost/"); var uri1 = new Uri(baseUri, "1/"); var uri2 = new Uri(uri1, "2"); 

Note: "part of the file name" is not a real term, since Url has only "path" and "request", but usually the last piece of the path is considered as the file name: "/ foo / bar / file. Text".

When you combine 2 paths, you want to replace part of the tail of the first path with the second. In your case, it ends up with only the “file name” segment for both: “/ 1” and “2” (if you put a real value like “/myFile.txt” and “NewFile.txt” when merging, it would be easier to understand why it behaves in this way).

+11
source

Source: https://habr.com/ru/post/913169/


All Articles