【问题标题】:Getting the current URL that is shown in the browser获取浏览器中显示的当前 URL
【发布时间】:2014-05-07 01:12:16
【问题描述】:

目前我正在尝试获取浏览器中显示的当前 URL。

如果我使用

Request.Path 

我得到https://this.website.com:443/Default.aspx,这在技术上是正确的。

但是浏览器本身显示的 URL 是https://this.website.com/

使用任何请求选项仍将显示 Default.aspx。

我需要最终检测浏览器中的 url 是 https://this.website.com 还是 http://this.website.com/Default.aspx,如果不存在则重定向到 Default.aspx。

顺便说一句,更复杂的是我的 web.config 中的 https 重定向。

【问题讨论】:

标签: c# asp.net url request


【解决方案1】:

您可以从httpcontext 中的请求中获取。

HttpContext.Current.Request.Url

更新:

如果你想知道当前的 url 是 / 还是 /default.aspx。您可以使用请求的RawUrl 属性。该字段将包含整个 url。

HttpContext.Current.Request.RawUrl

【讨论】:

  • 不,当浏览器窗口中的 url 为 this.website.com 时,它会显示 this.website.com/default.aspx
  • 你必须使用HttpContext.Current.Request.Url.Host
  • @DanielB 不。这只给了我主机。我需要确定网址是 this.site.com/ 还是 this.site.com/Default.aspx。
  • @Prescient 然后使用HttpContext.Current.Request.Url.AbsolutePath 进行检查?
  • 不。返回的是/Default.aspx,浏览器显示site.com
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-09
  • 2011-06-24
  • 2021-12-09
相关资源
最近更新 更多