C#如何使用Uri等于? [英] C# How to use Uri equal?

查看:123
本文介绍了C#如何使用Uri等于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些测试:

这是真的:

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

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

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

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

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

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

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

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

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

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

And in aspnet core mvc, if i use route

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

这是错误的!错误是: AmbiguousActionException:已匹配多个动作.以下操作与路线数据匹配,并且满足所有约束条件:

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

推荐答案

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

就协议而言, http://example.com/something http://example.com/something/完全不同.一些服务器可能如果以这种方式实现,则将您从一个重定向到另一个.

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.

对于纯域名,它总是发送一个以.结尾的请求削减.(域名本身不包含在一个HTTP请求,就像Greg Hewgill和其他人所写的一样.这是,但是,包含在标题中.)

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

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

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

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

从DNS解析的名称不区分大小写.这对防止混乱.

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

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

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

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

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.

这是错误的!错误是:AmbiguousActionException:多个动作匹配.以下操作与路线数据匹配,并具有所有满足约束条件:

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

这是因为ASP.NET MVC通常 不区分大小写.在ASP.NET MVC中强制区分大小写的路由可能是对于问题的这一部分很有用.

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.

这篇关于C#如何使用Uri等于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆