C#中Uri类的解释

URI,是uniform resource identifier,统一资源标识符,用来唯一的标识一个资源。而URL是uniform resource locator,统一资源定位器,它是一种具体的URI,即URL可以用来标识一个资源,而且还指明了如何locate这个资源。而URN,uniform resource name,统一资源命名,是通过名字来标识资源。 也就是说,URI是以一种抽象的,高层次概念定义统一资源标识,而URL和URN则是具体的资源标识的方式。

URI的抽象结构

//最基本的划分
[scheme:]scheme-specific-part[#fragment]  
//对scheme-specific-part进一步划分
[scheme:][//authority][path][?query][#fragment]  
//对authority再次划分, 这是最细分的结构
[scheme:][//host:port][path][?query][#fragment]  

下面通过代码展示Uri类如何获取上面各个部分的

Uri uriAddress = new Uri("http://www.aiaide.com:8080/Home/index.htm?a=1&b=2#search");
Console.WriteLine(uriAddress.Scheme);
Console.WriteLine(uriAddress.Authority);
Console.WriteLine(uriAddress.Host);
Console.WriteLine(uriAddress.Port);
Console.WriteLine(uriAddress.AbsolutePath);
Console.WriteLine(uriAddress.Query);
Console.WriteLine(uriAddress.Fragment);
//通过UriPartial枚举获取指定的部分
Console.WriteLine(uriAddress.GetLeftPart(UriPartial.Path));
//获取整个URI
Console.WriteLine(uriAddress.AbsoluteUri);

GetLeftPart 方法返回一个字符串,包含与指定的部分结束的 URI 字符串的最左侧部分, 具体含义参考下面的表格中UriPartial的枚举含义.

成员名称说明
Scheme

包括URI 的方案段。

Authority

包括URI 的方案段与颁发机构段。

Path

包括URI 的方案段、颁发机构段与路径段。

Query

包括URI 的方案段、证书颁发机构段、路径段与查询段。

原文地址:https://www.cnblogs.com/answercard/p/5197711.html