I am writing a C# application that needs to callout to a webapp. I am trying to use System.Uri
to combine two URL's: A base URL and the relative path to the specific service (or many) of the webapp I need. I have a class member named webappBackendURL
that I want to define in one place, with all calls building from it (webappBackendURL
is not defined as close as my examples illustrate).
System.Uri webappBackendURL = new System.Uri("http://example.com/");
System.Uri rpcURL = new System.Uri(webappBackendURL,"rpc/import");
// Result: http://example.com/rpc/import
This works, however, if webappBackendURL
contains a query string, it is not preserved.
System.Uri webappBackendURL = new System.Uri("http://example.com/?authtoken=0x0x0");
System.Uri rpcURL = new System.Uri(webappBackendURL,"rpc/import");
// Result: http://example.com/rpc/import <-- (query string lost)
Is there a better way combine URLs? The .NET library is extensive, so I'm thinking I might have just overlooked a built-in way to handle this. Ideally, I would like to be able to combine URLs like this:
System.Uri webappBackendURL = new System.Uri("http://example.com/?authtoken=0x0x0");
System.Uri rpcURL = new System.Uri(webappBackendURL,"rpc/import?method=overwrite&runhooks=true");
// Result: http://example.com/rpc/import?authtoken=0x0x0&method=overwrite&runhooks=true