Given a URI like:
/path/page/?param1=value1¶m2=value2
As it would be generated by using:
Url.Action("page","path", new { param1 = "value", param2 = "value2" })
What would be the cleanest way to strip the query string so it would result in /path/page/
?
After searching in SO specifically and a more wide Google search, the best answer I found was to create a Uri object and use uri.GetLeftPart(UriPartial.Path)
which I already know and use for absolute URIs.
Problem is, this will not work for relative URIs, and both doing
Uri uri = new Uri(new Uri("http://www.fakesite.com"), myRelativeUri)
string cleanUri = uri.AbsolutePath
and
string cleanUri = myRelativeUri.Substring(0, myRelativeUri.IndexOf('?'))
look sloppy.
I would use string clearnUri = myRelativeUri.Split('?')[0];
It's about as clean as you're going to get.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With