Uri 方法獲取 Protocal Host Port
Uri uri = new Uri("https://www.mywebsite.com:80/pages/page1.aspx"); string requested =$"{uri.Scheme} {Uri.SchemeDelimiter} { uri.Host} { uri.Port} {uri.AbsolutePath}"; Console.WriteLine(requested); // 【uri.Scheme/Request.Url.Scheme】 https // 【Uri.SchemeDelimiter/Request.Url.SchemeDelimiter】 :// // 【uri.Host/Request.Url.Host】 www.mywebsite.com // 【uri.Port/Request.Url.Port】 80 // 【Request.Url.AbsolutePath】 /pages/page1.aspx string aut = UriPartial.Authority.ToString(); Console.WriteLine(aut); string authority = uri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped); Console.WriteLine(authority); // 【Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped)】 https://www.mywebsite.com:80 可以灵活选择所需元素 string requested2 = $"{uri.GetLeftPart(UriPartial.Authority)}"; Console.WriteLine(requested2); //【uri.GetLeftPart(UriPartial.Authority)】 https://www.mywebsite.com:80