C# Uri.TryCreate未生成我预期的结果

C# Uri.TryCreate未生成我预期的结果,c#,uri,C#,Uri,我正在抓取一个网站的URL和锚标签都有他们的href值刚刚设置为querystring例如 <a href="?AppId=12345&CatId=13">Details</a> 因此,我正在寻找的网址将是 http://www.theurl.com/ThePage.aspx?AppId=12345&CatId=13 为了实现这一点,我使用了Uri.TryCreate方法,因此我传入了以下参数(前两个参数的类型是Uri,而不是string) 但是,ou

我正在抓取一个网站的URL和锚标签都有他们的href值刚刚设置为querystring例如

<a href="?AppId=12345&CatId=13">Details</a>
因此,我正在寻找的网址将是

http://www.theurl.com/ThePage.aspx?AppId=12345&CatId=13
为了实现这一点,我使用了Uri.TryCreate方法,因此我传入了以下参数(前两个参数的类型是Uri,而不是string)

但是,out参数“uri”被设置为

http://www.theurl.com/?AppId=12345&CatId=13
如您所见,它删除了.aspx路径。你能推荐一个更好的方法来做这件事,或者解释为什么它不能像我认为的那样工作吗?

试试这个:

Uri.TryCreate("http://www.theurl.com/ThePage.aspx?PageNo=2", "ThePage.aspx?AppId=12345&CatId=13", out uri);

根据,第一个是基本URI,第二个是相对URI。

嗯。我不确定发生的行为是否正确。这很可能是一个错误

在任何情况下,您可能会发现以下更方便:

UriBuilder uBuild = new UriBuilder("http://www.theurl.com/path/thePage.aspx?PageNo=2");
uBuild.Query = "AppId=12345&CatId=13";
Uri newUri = ub.Uri;//http://www.theurl.com/path/thePage.aspx?AppId=12345&CatId=13
//Note that we can reuse uBuild as we continue to parse the page, as long as we're only dealing with cases where only the query changes.
uBuild.Query = "AppId=678&CatId=2";
Uri anotherUri = ub.Uri;//http://www.theurl.com/path/thePage.aspx?AppId=678&CatId=2
Uri.TryCreate("http://www.theurl.com/ThePage.aspx?PageNo=2", "ThePage.aspx?AppId=12345&CatId=13", out uri);
UriBuilder uBuild = new UriBuilder("http://www.theurl.com/path/thePage.aspx?PageNo=2");
uBuild.Query = "AppId=12345&CatId=13";
Uri newUri = ub.Uri;//http://www.theurl.com/path/thePage.aspx?AppId=12345&CatId=13
//Note that we can reuse uBuild as we continue to parse the page, as long as we're only dealing with cases where only the query changes.
uBuild.Query = "AppId=678&CatId=2";
Uri anotherUri = ub.Uri;//http://www.theurl.com/path/thePage.aspx?AppId=678&CatId=2