为什么 Equals 方法中会忽略 Uri 的片段?

发布于 2024-08-04 05:09:06 字数 1243 浏览 2 评论 0原文

我试图根据 URI 维护对象的集合:

public class ConceptCollection : KeyedCollection<Uri, Concept> {
    protected override Uri GetKeyForItem(Concept item) {
        return item.Uri;
    } 
}

但是,URI 通常仅根据 Uri 的片段而有所不同。因此,以下内容会导致错误:

ConceptCollection wines = new ConceptCollection();
Concept red = new Concept("http://www.w3.org/2002/07/owl#RedWine");
Concept white = new Concept("http://www.w3.org/2002/07/owl#WhiteWine");
wines.Add(red);
wines.Add(white); // Error: An item with the same key has already been added.

Per http://msdn.microsoft.com/ en-us/library/f83xtf15.aspx

Equals方法比较两者 不考虑用户的实例 信息(UserInfo)和片段( 片段)他们可能的部分 包含。例如,给定 URI http://www.contoso.com/index.htm#searchhttp://user: [电子邮件受保护]/index.htm, Equals 方法将返回 true。

我已经接受了必须解决这个问题。但为什么它会这样呢?我可以看到用户信息的逻辑,但看不到片段的逻辑。

I'm trying to maintain a collection of objects based on their URI:

public class ConceptCollection : KeyedCollection<Uri, Concept> {
    protected override Uri GetKeyForItem(Concept item) {
        return item.Uri;
    } 
}

However, the URI regularly only differs based on the Fragment of the Uri. So, the following causes an error:

ConceptCollection wines = new ConceptCollection();
Concept red = new Concept("http://www.w3.org/2002/07/owl#RedWine");
Concept white = new Concept("http://www.w3.org/2002/07/owl#WhiteWine");
wines.Add(red);
wines.Add(white); // Error: An item with the same key has already been added.

Per http://msdn.microsoft.com/en-us/library/f83xtf15.aspx:

The Equals method compares the two
instances without regard to user
information ( UserInfo) and fragment (
Fragment) parts that they might
contain. For example, given the URIs
http://www.contoso.com/index.htm#search
and
http://user:[email protected]/index.htm,
the Equals method would return true.

I'm resigned to having to hack around this. But why does it behave this way? I can see the logic for user-info, but not for fragment.

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

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

发布评论

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

评论(2

傲性难收 2024-08-11 05:09:06

来自 RFC 2396

4.1。片段标识符

当 URI 引用用于对标识的资源执行检索操作时,可选片段标识符(通过剖面线(“#”)字符与 URI 分隔)包含由用户代理解释的附加引用信息后
检索操作已成功完成。 因此,它不是 URI 的一部分,但经常与 URI 结合使用。

添加的强调是我的,也是在 Uri.Equals 实现中不考虑该片段的原因。

在您的示例中,您要检索的资源的 URI 为: http://www.w3.org /2002/07/owl

片段由用户代理处理,对资源的实际检索没有任何意义或影响。

From RFC 2396:

4.1. Fragment Identifier

When a URI reference is used to perform a retrieval action on the identified resource, the optional fragment identifier, separated from the URI by a crosshatch ("#") character, consists of additional reference information to be interpreted by the user agent after
the retrieval action has been successfully completed. As such, it is not part of a URI, but is often used in conjunction with a URI.

The emphasis added is mine and is the reason the fragment is not considered in the Uri.Equals implementation.

In your example, the URI for the resource you are retrieving is: http://www.w3.org/2002/07/owl

The fragments are processed by the user agent and have no meaning to or influence on the actual retrieval of the resource.

同展鸳鸯锦 2024-08-11 05:09:06

我猜是因为除了片段之外的 2 个相同的 URI 仍然引用相同的资源,只是资源中的不同位置。

因此,如果您问“这些是相同的资源吗?”那么你可能会认为忽略该片段是正确的。

I guess because 2 URIs that are identical apart from the fragment still refer to the same resource, just a different location within the resource.

So if you're asking the question 'are these the same resource?' then you could argue that it's correct to ignore the fragment.

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