.NET - Get protocol, host, and port

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

    Well if you are doing this in Asp.Net or have access to HttpContext.Current.Request I'd say these are easier and more general ways of getting them:

    var scheme = Request.Url.Scheme; // will get http, https, etc.
    var host = Request.Url.Host; // will get www.mywebsite.com
    var port = Request.Url.Port; // will get the port
    var path = Request.Url.AbsolutePath; // should get the /pages/page1.aspx part, can't remember if it only get pages/page1.aspx
    

    I hope this helps. :)

    0 讨论(0)
提交回复
热议问题