.NET - Get protocol, host, and port

后端 未结 7 934
情歌与酒
情歌与酒 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:25

    Request.Url will return you the Uri of the request. Once you have that, you can retrieve pretty much anything you want. To get the protocol, call the Scheme property.

    Sample:

    Uri url = Request.Url;
    string protocol = url.Scheme;
    

    Hope this helps.

提交回复
热议问题