I would like to modify the NavigateUrl property of a Hyperlink control. I need to preserve the querystring but change the path of the hyperlink’s URL.
Something along these lines:
var control = (Hyperlink) somecontrol; // e.g., control.NavigateUrl == "http://www.example.com/path/to/file?query=xyz" var uri = new Uri(control.NavigateUrl); uri.AbsolutePath = "/new/absolute/path"; control.NavigateUrl = uri.ToString(); // control.NavigateUrl == "http://www.example.com/new/absolute/path?query=xyz"
Uri.AbsolutePath is read-only (no setter defined), though, so this solution won’t work.
How would I change just the path of a Hyperlink‘s NavigateUrl property while leaving the querystring, hostname and schema parts intact?
You may find the UriBuilder class useful:
or to make it a little more generic:
and then: