【发布时间】:2012-10-13 15:05:05
【问题描述】:
我正在编写一个需要调用 web 应用程序的 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
但是,如果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)
有没有更好的方法来组合 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
【问题讨论】: