如何将URL与保留的查询字符串零件相结合



我正在编写一个C#应用程序,该应用程序需要向WebApp注释。我正在尝试使用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

您可以做这样的事情:

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("?", "&"));

尝试 UriTemplate

Uri baseUrl = new Uri("http://www.example.com");
UriTemplate template = new UriTemplate("/{path}/?authtoken=0x0x0");
Uri boundUri = template.BindByName(
    baseUrl,
    new NameValueCollection {{"path", "rpc/import"}});

System.UriTemplate在不同版本的.NET之间移动。您需要确定适合您项目的组装参考。

System.Uri webappBackendURL = new System.Uri("http://example.com/?authtoken=0x0x0");
System.Uri rpcURL           = new System.Uri(webappBackendURL,string.Format("rpc/import?{0}method=overwrite&runhooks=true", string.IsNullOrWhiteSpace(webappBackendURL.Query) ? "":webappBackendURL.Query + "&"));

尽管我可能会创建一种方法,该方法需要两个URI并在那里进行处理,以使其看起来更干净。

public static Uri MergerUri(Uri uri1, Uri uri2)
    {
        if (!string.IsNullOrWhiteSpace(uri1.Query))
        {
            string[] split = uri2.ToString().Split('?');
            return new Uri(uri1, split[0] + uri1.Query + "&" + split[1]);
        }
        else return new Uri(uri1, uri2.ToString());
    }
var originalUri = new Uri("http://example.com?param1=1&param2=2");
var resultUri = new Uri(new Uri(originalUri, "/something"), originalUri.Query);

Resulturi:http://example.com/something?

使用收到的答案中的想法和作品,我写了一种包装方法,可以准确地实现了我想要的东西。我希望核心.NET类能够处理这一点,但是看起来它们不能。

此方法将使用完整的URL(http://example.com?path?query=string),并将relative/path?query=string&with=arguemnts形式的相对URL(字符串)组合到其中。

private static System.Uri ExtendURL(System.Uri baseURL, string relURL)
{
    string[] parts = relURL.Split("?".ToCharArray(), 2);
    if (parts.Length < 1)
    {
        return null;
    }
    else if (parts.Length == 1)
    {
        // No query string included with the relative URL:
        return new System.Uri(baseURL,
                              parts[0] + baseURL.Query);
    }
    else
    {
        // Query string included with the relative URL:
        return new System.Uri(baseURL,
                              parts[0] + (String.IsNullOrWhiteSpace(baseURL.Query) ? "?" : baseURL.Query + "&") + parts[1]);
    }
}
ExtendURL(new System.Uri("http://example.com/?authtoken=0x0x0"),"rpc/import?method=overwrite&runhooks=true");
// Result: http://example.com/rpc/import?authtoken=0x0x0&method=overwrite&runhooks=true

感谢所有贡献的人!我向您的所有答案投入了。

最新更新