6

webapp へのコールアウトが必要な C# アプリケーションを作成しています。System.Uriベース URL と、必要な Web アプリケーションの特定のサービス (ま​​たは多数) への相対パスの 2 つの URL を組み合わせて使用​​しようとしています。webappBackendURL1 か所で定義したいという名前のクラス メンバーがあり、そこからすべての呼び出しが作成されます (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
4

5 に答える 5

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("?", "&"));
于 2012-10-23T16:04:27.823 に答える
1
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 + "&"));

おそらく、2 つの 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());
    }
于 2012-10-23T16:14:14.793 に答える
1

試してください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 の異なるバージョン間を移動しました。どのアセンブリ参照がプロジェクトに適しているかを判断する必要があります。

于 2012-10-23T16:15:48.587 に答える
0

受け取った回答のアイデアと断片を使用して、探していたものを正確に実現するラッパー メソッドを作成しました。コア .NET クラスがこれを処理できることを願っていましたが、できないようです。

このメソッドは、完全な URL ( http://example.com?path?query=string) を受け取り、相対 URL (文字列) を の形式で結合しますrelative/path?query=string&with=arguemnts

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

貢献してくれたすべての人に感謝します!私はあなたのすべての答えに賛成しました。

于 2012-10-23T18:42:33.557 に答える