C# How to use Uri equal?

好久不见. 提交于 2020-01-05 08:09:10

问题


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:


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.



来源:https://stackoverflow.com/questions/47746053/c-sharp-how-to-use-uri-equal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!