Uri.GetComponents return query params even if its not requested

77 views Asked by At

I am trying to extract the path of my PathString without the query parameters. So I did the following:

var ps = new PathString("/someapi/wayne/insane?param=1234&ups=134");
var u = new UriBuilder();
u.Path = ps;
// Built Uri: http://localhost/someapi/wayne/insane%3Fparam=1234&ups=134
u.Uri.GetComponents(UriComponents.Path, UriFormat.Unescaped).Dump();

// returns: "someapi/wayne/insane?param=1234&ups=134"

I'd have expected it will return: /someapi/wayne/insane Is it supposed to work that way? Is there another way to get just the path?

I found this: Get url without querystring, pointing out to use GetLeftPart, which resulted in the same string.

1

There are 1 answers

0
greg-e On

Maybe not the most elegant way to do it, but it works for all cases I can think of for now:

var ps = new PathString("/someapi/wayne/insane?param=1234&ups=134");
var u = new Uri("http://localhost" + ps.Value);
u.LocalPath.Dump();

Interestingly the PathString needs to be resolved to string by using the Value property.