C# 如何将URL与保留的查询字符串部分组合?
我正在编写一个C#应用程序,需要调用到一个webapp。我正在尝试使用C# 如何将URL与保留的查询字符串部分组合?,c#,url,C#,Url,我正在编写一个C#应用程序,需要调用到一个webapp。我正在尝试使用System.Uri组合两个URL:一个基本URL和我需要的webapp的特定服务(或多个)的相对路径。我有一个名为webappBackendURL的类成员,我想在一个地方定义它,所有调用都从它生成(webappBackendURL的定义没有我的示例所示的那么接近) 但是,如果webappBackendURL包含查询字符串,则不会保留该字符串 System.Uri webappBackendURL = new System.U
System.Uri
组合两个URL:一个基本URL和我需要的webapp的特定服务(或多个)的相对路径。我有一个名为webappBackendURL
的类成员,我想在一个地方定义它,所有调用都从它生成(webappBackendURL
的定义没有我的示例所示的那么接近)
但是,如果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)
你可以这样做:
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("?", "&"));
尽管我可能会创建一个包含两个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());
}
请尝试使用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的不同版本之间移动。您需要确定哪个程序集引用对您的项目是正确的。使用收到的答案中的想法和片段,我编写了一个包装器方法,它完全实现了我想要的。我希望核心的.NET类能够处理这个问题,但看起来他们做不到
此方法将获取完整的URL(http://example.com?path?query=string
)并将相对URL(字符串)以相对/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
private static System.Uri extendur(System.Uri baseURL,string relURL)
{
string[]parts=relURL.Split(“?”.ToCharArray(),2);
如果(零件长度<1)
{
返回null;
}
否则如果(parts.Length==1)
{
//相对URL中不包含查询字符串:
返回新的System.Uri(baseURL,
部件[0]+baseURL.Query);
}
其他的
{
//包含在相对URL中的查询字符串:
返回新的System.Uri(baseURL,
部分[0]+(String.IsNullOrWhiteSpace(baseURL.Query)?“?”:baseURL.Query+”&“)+部分[1]);
}
}
ExtendURL(新的System.Uri(“http://example.com/?authtoken=0x0x0“,”rpc/import?method=overwrite&runhooks=true“;
//结果:http://example.com/rpc/import?authtoken=0x0x0&method=overwrite&runhooks=true
感谢所有做出贡献的人!我对你所有的答案都投了赞成票
var originalUri = new Uri("http://example.com?param1=1¶m2=2");
var resultUri = new Uri(new Uri(originalUri, "/something"), originalUri.Query);
结果:当我试图从我的简单路径构造一个Uri
时,我遇到了一个UriFormatException
,因此我认为我不能可靠地使用MergerUri
。我可能会尝试将您最初的建议打包成一个方法。很抱歉,我没有首先测试它。更新后的一个可以工作,但与您决定的类似。我只使用了一个参数,因为它与您要求的最接近,但是您可以有多个(/{x}/{y}/foo)的模板。显然,每个占位符在NameValueCollection中都有一个条目。
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
var originalUri = new Uri("http://example.com?param1=1¶m2=2");
var resultUri = new Uri(new Uri(originalUri, "/something"), originalUri.Query);