C# How to use Uri equal?

2019-08-14 02:42发布

Some Test:

This is Ture:

    [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
    }

This is Ture:

    [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
    }

This is False:

    [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
    }

This is 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
    }

This is True:

    [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
    }

So,The Host not case sensitive? but path case sensitive??

And I want all Uri dot not case sensitive and dot not case '/',What should I do?

And I want all Uri dot not case sensitive and dot not case '/',What should I do?

And I want all Uri dot not case sensitive and dot not case '/',What should I do?

And in aspnet core mvc, if i use route

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

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

1条回答
走好不送
2楼-- · 2019-08-14 03:23

https://stackoverflow.com/a/2581418/34092 states:

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.)

So, looking at your examples:

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

They are the same, since always sends a request ending with a slash. They are thus equivalent.

https://serverfault.com/a/261344 states:

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/");

Thus, the two are equivalent (since they differ only by case and the slash immediately after the host).

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

OK, this seems like the first scenario, but it isn't. The first scenario treats them as equivalent since it is 'pure domain name' (i.e. straight after the host). This is different (i.e. the slash is at the end, not straight after the host), and thus they aren't equivalent (on all web servers). Thus not equal.

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

The path and querystring are case sensitive. Thus these are not equal. Some web servers / programming environments (e.g. ASP.NET MVC) may act case-insensitive, but according to the spec the path and querystring are case sensitive (since some web servers are case sensitive).

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

The only difference is the case of the host. Thus they are equal.

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

This is because ASP.NET MVC is generally not case sensitive. Force case-sensitive routing in ASP.NET MVC may be useful for this part of your problem.

查看更多
登录 后发表回答