【问题标题】:How to get the base URL of the website VB.NET如何获取网站 VB.NET 的基本 URL
【发布时间】:2012-09-16 02:51:51
【问题描述】:

我可能不是第一个提出这个问题的人,但我环顾四周后找不到我想要的东西。 我想从 URL 中获取基本 URL。我试过了

    HttpContext.Current.Request.Url.AbsoluteUri

它会返回完整的 URL

    http://localhost:59112/Resources/VideoPlayer.aspx?ID=resources1.mp4

我只需要到这里(例如)

    http://localhost:59112/

感谢您的帮助。

【问题讨论】:

    标签: url hostname domain-name base-url


    【解决方案1】:

    通过更多的研究..我从论坛中得到了我想要的..这是一个绝妙的解决方案..

        Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath
    

    谢谢

    【讨论】:

      【解决方案2】:

      网址前:http://localhost:59112/Resources/VideoPlayer.aspx?ID=resources1.mp4

      HttpContext.Current.Request.Url.Authority
      

      返回:“本地主机:59112”

      HttpContext.Current.Request.Url.Scheme & "://" & HttpContext.Current.Request.Url.Authority
      

      返回:“http://localhost:59112

      【讨论】:

        【解决方案3】:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim address As Uri = New Uri("http://stackoverflow.com/questions/12568530/how-to-get-the-base-url-of-the-website-vb-net")
            MsgBox("http://" & address.Host)
        End Sub
        

        【讨论】:

          【解决方案4】:

          在 .Net 4 及更高版本中,您可以使用 -

          Dim Url as String = Request.Url.OriginalString
          Dim Domain as String = Url.Replace(Request.PathAndQuery, "")
          
          Output -
          Url = "http://localhost:9898/content/page.aspx
          Domain = "http://localhost:9898"
          

          【讨论】:

            【解决方案5】:

            HttpContext.Current.Request.Url.Host

            【讨论】:

            • 这只返回主机名,没有端口和协议。
            • 但这就是我想要的。
            猜你喜欢
            • 2020-08-25
            • 1970-01-01
            • 2016-12-12
            • 1970-01-01
            • 2011-02-02
            • 1970-01-01
            • 1970-01-01
            • 2012-08-13
            • 2019-02-09
            相关资源
            最近更新 更多