0

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.

Charlieface
  • 52,284
  • 6
  • 19
  • 43
greg-e
  • 374
  • 4
  • 18
  • Well you are using the `UriBuilder` wrong. The `Path` property should only have the actual path, not query parameters inside it. It seems it's not actually verifying that. If you set the `Query` property separately you get correct results https://dotnetfiddle.net/9HdGwU – Charlieface May 23 '23 at 11:21

1 Answers1

0

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.

greg-e
  • 374
  • 4
  • 18