How to get the parameter from a relative URL string in C#?
While many of the URI operations are unavailable for UriKind.Relative
(for whatever reason), you can build a fully qualified URI through one of the overloads that takes in a Base URI
Here's an example from the docs on Uri.Query
:
Uri baseUri = new Uri ("http://www.contoso.com/");Uri myUri = new Uri (baseUri, "catalog/shownew.htm?date=today");Console.WriteLine(myUri.Query); // date=today
You can also get the current base from HttpContext.Current.Request.Url
or even just create a mock URI base with "http://localhost"
if all you care about is the path components.
So either of the following approaches should also return the QueryString from a relative path:
var path = "catalog/shownew.htm?date=today"var query1 = new Uri(HttpContext.Current.Request.Url, path).Query;var query2 = new Uri(new Uri("http://localhost"), path).Query;