如何将URL与查询字符串部分组合保留?

jim*_*imp 5 c# url

我正在编写一个需要调用webapp的C#应用​​程序.我试图用来System.Uri组合两个URL:一个基本URL和我需要的webapp的特定服务(或许多)的相对路径.我有一个名为webappBackendURL我想在一个地方定义的类成员,所有调用都是从它构建的(webappBackendURL未定义为我的示例所示的接近).

System.Uri webappBackendURL = new System.Uri("http://example.com/");
System.Uri rpcURL           = new System.Uri(webappBackendURL,"rpc/import");
// Result: http://example.com/rpc/import
Run Code Online (Sandbox Code Playgroud)

但是,如果webappBackendURL包含查询字符串,则无法保留.

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)
Run Code Online (Sandbox Code Playgroud)

是否有更好的方法组合URL?.NET库很广泛,所以我想我可能只是忽略了一种内置的方法来处理这个问题.理想情况下,我希望能够组合这样的URL:

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
Run Code Online (Sandbox Code Playgroud)

Bra*_*rad 2

你可以这样做:

System.Uri webappBackendURL = 
  new System.Uri("http://example.com/?authtoken=0x0x0");
System.Uri rpcURL = new System.Uri(webappBackendURL,
  "rpc/import?ethod=overwrite&runhooks=true" 
  + webappBackendURL.Query.Replace("?", "&"));
Run Code Online (Sandbox Code Playgroud)