.NET - 获取协议,主机和端口 [英] .NET - Get protocol, host, and port

查看:867
本文介绍了.NET - 获取协议,主机和端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有在.NET中一个简单的方法来快速获取当前协议,主机和端口?举例来说,如果我在以下网址:

Is there a simple way in .NET to quickly get the current protocol, host, and port? For example, if I'm on the following URL:

<一个href="http://www.mywebsite.com:80/pages/page1.aspx">http://www.mywebsite.com:80/pages/page1.aspx

我需要返回:

http://www.mywebsite.com:80

我知道我可以使用Request.Url.AbsoluteUri来获得完整的网址,我知道我可以使用Request.Url.Authority获取主机和端口,但我不知道的,以获得最好的方法协议没有解析出的URL字符串。

I know I can use Request.Url.AbsoluteUri to get the complete URL, and I know I can use Request.Url.Authority to get the host and port, but I'm not sure of the best way to get the protocol without parsing out the URL string.

有什么建议?

推荐答案

以下(C#)code应该做的伎俩

The following (C#) code should do the trick

Uri uri = new Uri("http://www.mywebsite.com:80/pages/page1.aspx");
string requested = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port;

这篇关于.NET - 获取协议,主机和端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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