Uri.TryCreate 没有产生我期望的结果

发布于 2024-12-28 17:22:01 字数 710 浏览 1 评论 0原文

我正在抓取一个网站的 URL,并且锚标记都将其 href 值设置为查询字符串,例如..

<a href="?AppId=12345&CatId=13">Details</a>

当前页面的 URL 是这样的..

http://www.theurl.com/ThePage.aspx?PageNo=2

因此我正在寻找的 url 将是

http://www.theurl.com/ThePage.aspx?AppId=12345&CatId=13

To get this我正在使用 Uri.TryCreate 方法,因此我传递以下参数(前两个参数是 Uri 类型而不是字符串)。

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

但是,输出参数“uri”被设置为...

http://www.theurl.com/?AppId=12345&CatId=13

正如你可以的看到它删除了.aspx 路径。您能否推荐一种更好的方法来执行此操作或解释为什么它不能像我想象的那样工作?

I'm scraping a website for URL's and the anchor tags all have their href value just set to the querystring eg..

<a href="?AppId=12345&CatId=13">Details</a>

The URL of the current page is something like this..

http://www.theurl.com/ThePage.aspx?PageNo=2

Therefore the url I'm looking for will be

http://www.theurl.com/ThePage.aspx?AppId=12345&CatId=13

To get this I am using the the method Uri.TryCreate, so I'm passing in the following parameters (the first two parameters are of type Uri and not string)..

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

However the out parameter 'uri' is being set to..

http://www.theurl.com/?AppId=12345&CatId=13

As you can see it removes the .aspx path. Can you recommend a better way to do this or explain why it doesn't work as I thought it should?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城凉梦凉人心 2025-01-04 17:22:01

试试这个:

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

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

Try this:

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

According to the docs, the first one is the base URI, the second is the relative URI.

不如归去 2025-01-04 17:22:01

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

无论如何,您可能会发现以下方法更方便:

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

Hmm. I'm not sure the behaviour that happens is correct. That may well be a bug.

In any case, you might find the following handier:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文