gpt4 book ai didi

C#如何使用Uri equal?

转载 作者:太空宇宙 更新时间:2023-11-03 22:50:13 25 4
gpt4 key购买 nike

一些测试:

这是真的:

    [Fact]
public void UriEqualTest()
{
//Act
var uri1 = new Uri("https://www.baidu.com");
var uri2 = new Uri("https://www.baidu.com/");

var boolResult = uri2.Equals(uri1);

//Assert
Assert.Equal(uri1, uri2);
Assert.True(boolResult);//True
}

这是真的:

    [Fact]
public void UriUpperEqualTest()
{
//Act
var uri1 = new Uri("https://wWw.bAidu.com");
var uri2 = new Uri("https://www.baidu.com/");

var boolResult = uri2.Equals(uri1);

var operatorResult = (uri1 == uri2);

//Assert
Assert.Equal(uri1, uri2);
Assert.True(boolResult);//True
}

这是错误的:

    [Fact]
public void UrlEqualTest()
{
//Act
var uri1 = new Uri("https://www.baidu.com/aaaa/bbbb");
var uri2 = new Uri("https://www.baidu.com/aaaa/bbbb/");

var boolResult = uri2.Equals(uri1);

//Assert
Assert.Equal(uri1, uri2);
Assert.True(boolResult);//False
}

这是错误的:

    [Fact]
public void UrlUpperEqualTest()
{
//Act
var uri1 = new Uri("https://www.baidu.com/AAaa/bbbb");
var uri2 = new Uri("https://www.baidu.com/aAAa/bbbb");

var boolResult = uri2.Equals(uri1);
var operatorResult = (uri1 == uri2);
//Assert
Assert.Equal(uri1, uri2);
Assert.True(boolResult);//False
}

这是真的:

    [Fact]
public void UriUpperEqualAndPathTest()
{
//Act
var uri1 = new Uri("https://www.baiDu.com/aaaa/bbbb");
var uri2 = new Uri("https://www.Baidu.com/aaaa/bbbb");

var boolResult = uri2.Equals(uri1);

//Assert
Assert.Equal(uri1, uri2);
Assert.True(boolResult);//True
}

那么,主机不区分大小写?但路径区分大小写??

而且我希望所有的 Uri 点不区分大小写,点不区分大小写 '/',我该怎么办?

而且我希望所有的 Uri 点不区分大小写,点不区分大小写 '/',我该怎么办?

而且我希望所有的 Uri 点不区分大小写,点不区分大小写 '/',我该怎么办?

在aspnet core mvc中,如果我使用路由

[HttpGet("/private/head")] 和 [HttpGet("/private/HeAd")] 和 [HttpGet("/private/head/")]

错了!错误是:AmbiguousActionException:多个 Action 匹配。以下操作匹配路由数据并满足所有约束:

最佳答案

https://stackoverflow.com/a/2581418/34092状态:

As far as the protocol is concerned, http://example.com/something and http://example.com/something/ are quite different. Some servers might redirect you from one to the other if it is implemented in such a way.

As for the pure domain names, it always sends a request ending with a slash. (The domain name itself is not included in the path section of an HTTP request, just as Greg Hewgill and the others wrote. It is, however, included in the headers.)

所以,看看你的例子:

    var uri1 = new Uri("https://www.baidu.com");
var uri2 = new Uri("https://www.baidu.com/");

它们是相同的,因为总是发送以斜杠结尾的请求。因此它们是等价的。

https://serverfault.com/a/261344状态:

Names resolved from DNS are case insensitive. This is important to prevent confusion.

    var uri1 = new Uri("https://wWw.bAidu.com");
var uri2 = new Uri("https://www.baidu.com/");

因此,两者是等价的(因为它们仅在大小写和主机后面的斜线不同)。

    var uri1 = new Uri("https://www.baidu.com/aaaa/bbbb");
var uri2 = new Uri("https://www.baidu.com/aaaa/bbbb/");

好吧,这似乎是第一种情况,但事实并非如此。第一种情况将它们视为等同的,因为它是“纯域名”(即直接在主机之后)。这是不同的(即斜杠在末尾,而不是紧跟在主机之后),因此它们不是等价的(在所有 Web 服务器上)。因此不相等。

    var uri1 = new Uri("https://www.baidu.com/AAAaa/bbbb");
var uri2 = new Uri("https://www.baidu.com/aAAa/bbbb");

路径和查询字符串区分大小写。因此这些不相等。某些网络服务器/编程环境(例如 ASP.NET MVC)可能不区分大小写,但根据规范,路径和查询字符串区分大小写(因为某些网络服务器区分大小写)。

    var uri1 = new Uri("https://www.baiDu.com/aaaa/bbbb");
var uri2 = new Uri("https://www.Baidu.com/aaaa/bbbb");

唯一的区别是主机的大小写。因此它们是相等的。

It's error! the error is: AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:

这是因为 ASP.NET MVC 通常不区分大小写。 Force case-sensitive routing in ASP.NET MVC可能对您的这部分问题有用。

关于C#如何使用Uri equal?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47746053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com