【问题标题】:Incorrect new Uri (base, relative)不正确的新 Uri(基础,相对)
【发布时间】:2013-06-11 05:42:10
【问题描述】:

我正在开发通用网店刮刀,但遇到了奇怪的问题。实际上,我需要网页上列出的所有产品的正确 URL。大多数时候,这些产品的 href 是相对的。

我正在使用新的 Uri 方法来创建完整的产品 URL。

new Uri (base, href) 

//this actually decide to add "/" before product href

href = x.ProductHref.IsUrlAbsolute() ? x.ProductHref : ((x.ProductHref.StartsWith("/") || (x.ProductHref.StartsWith("."))) ? x.ProductHref : "/" + x.ProductHref)

失败的结果:如何纠正这个问题

Base URL: "www.bebitalia.it/Products/ProductList_it.html#filter&.CHAISE_LONGUE"
Product href: Product-landscape-chaise_longue_118_it.html
Result : http://www.bebitalia.it/Product-landscape-chaise_longue_118_it.html ==> Status Wrong
Expected:  http://www.bebitalia.it/Products/Product-landscape-chaise_longue_118_it.html

我试图纠正它,但它不适用于其他人:)。想让它通用。请提出任何解决方案,我需要正确的方向或更好的方法。

【问题讨论】:

  • 肯定只是:"/Products/" + x.ProductHref

标签: c# uri


【解决方案1】:

伙计们,我解决了它。现在它是通用的,适用于所有场景。

PageURL 是基本网址

href = (x.ProductHref.IsUrlAbsolute() || (!x.ProductHref.Contains("/") && !PageUrl.AbsoluteUri.EndsWith("/")) ? x.ProductHref : ( (x.ProductHref.StartsWith("/") || (x.ProductHref.StartsWith("."))) ? x.ProductHref : "/" + x.ProductHref))

Url = 新 Uri(PageUrl, href)

我提取了一条规则 =>如果基本 url 不以“/”结尾,并且 productURL(relative) 也不包含“/”而 base 是绝对的,那么永远不要将“/”添加到 relative。

我不知道浏览器如何自动解决这种情况。 它必须自动解决 创建新 uri(基础,相对)时。

【讨论】:

    【解决方案2】:

    任何以/ 开头的链接都被视为位于域的根目录中。因此,首先,为了清楚起见,我将重新格式化您的代码:

    href = x.ProductHref.IsUrlAbsolute() 
           ? x.ProductHref 
           : (x.ProductHref.StartsWith("/") || (x.ProductHref.StartsWith("."))
                ? x.ProductHref 
                : "/" + x.ProductHref;
    

    将其简化为评估方式:

    href = false 
           ? // Nothing 
           : (false || false)
                ? // Nothing 
                : "/" + "Product-landscape-chaise_longue_118_it.html";
    

    这会生成 URL /Product-landscape-chaise_longue_118_it.html,它以 / 开头,因此位于您域的根目录中。

    如何解决此问题完全取决于您未提供的其余代码,因此我无法提供任何建议。

    【讨论】:

    • 我的解决方案不是通用的,事实上这就是我没有提供它的原因。之后它失败了;基本 URl= caliroots.com/sfd 产品 href:./product.asp?id=42952 结果:caliroots.com/sfd/product.asp?id=42952 状态正常
    • 基本网址:shop.mango.com/DK/mango/new 产品链接:DK/p0/mango/new/chiffon-baby-doll-dress/?id=83439925_OW&n=1&s=nuevo&ie=0&m=&ts=1371208214121 结果:@ 987654324@ 状态正常 基本 URL:zalando.it/scarpe-con-tacco 产品 href:oxitaly-decollete-nero-ox211a01d-802.html 结果:zalando.it/oxitaly-decollete-nero-ox211a01d-802.html 状态正常
    • @JehangirHaider - 那么你的问题是数据错误。 GIGO。您需要建立一个约定:“如果您以/ 开头产品href,它会这样做。如果您以./ 开头它会this。如果你不放无论哪种方式,它都会做其他事情。”然后为该约定编写代码。
    【解决方案3】:

    你应该知道如果你在new Uri()之后改变href,它不会改变结果,但我认为是在你调用构造函数之前。

    什么是碱基? www.bebitalia.it/Products/ProductList_it.html#filter&.CHAISE_LONGUE 不是有效的 Uri 并且 Uri 没有带有 2 个字符串参数的构造函数。

    如果 href 是绝对的,构造函数new Uri(Uri,String) 将自动处理它,并放置/。它还会在 TLD 之后切断基本 Uri 中的所有内容(这可能就是您的代码中发生的情况)。

    HERE你可以看看它是如何工作的(它是MSDN示例)

    【讨论】:

    • www.bebitalia.it/Products/ProductList_it.html#filter&.CHAISE_LONGUE 是基本 URI
    猜你喜欢
    • 2013-02-28
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 1970-01-01
    • 2018-04-08
    相关资源
    最近更新 更多