.NET - Get protocol, host, and port

后端 未结 7 936
情歌与酒
情歌与酒 2020-11-28 19:18

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:

http://www.mywebsite.com:80/pages

7条回答
  •  醉梦人生
    2020-11-28 19:34

    Even shorter way, may require newer ASP.Net:

    string authority = Request.Url.GetComponents(UriComponents.SchemeAndServer,UriFormat.Unescaped)
    

    The UriComponents enum lets you specify which component(s) of the URI you want to include.

提交回复
热议问题