获取用户在浏览器中输入的确切 url

发布于 2024-07-17 09:04:01 字数 832 浏览 7 评论 0原文

我想获取用户在浏览器中输入的确切网址。 当然,我总是可以使用像 Request.Url.ToString() 这样的东西,但这并不能在以下情况下给我我想要的东西:

http://www.mysite.com/rss

上面的 url Request.Url.ToString() 会给我的是:

http://www.mysite.com/rss/Default.aspx

有谁知道如何做到这一点?

我已经尝试过:

  • Request.Url
  • Request.RawUrl
  • this.Request.ServerVariables["CACHE_URL"]
  • this.Request.ServerVariables [“HTTP_URL”]
  • ((HttpWorkerRequest)((IServiceProvider)HttpContext.Current).GetService(typeof(HttpWorkerRequest))).GetServerVariable(“CACHE_URL”)
  • ((HttpWorkerRequest )((IServiceProvider)HttpContext.Current).GetService(typeof(HttpWorkerRequest))).GetServerVariable(“HTTP_URL”)

I would like to get the exact url that user typed into the browser. Of course I could always use something like Request.Url.ToString() but this does not give me what i want in the following situation:

http://www.mysite.com/rss

With the url above what Request.Url.ToString() would give me is:

http://www.mysite.com/rss/Default.aspx

Does anyone know how to accomplish this?

I have already tried:

  • Request.Url
  • Request.RawUrl
  • this.Request.ServerVariables["CACHE_URL"]
  • this.Request.ServerVariables["HTTP_URL"]
  • ((HttpWorkerRequest)((IServiceProvider)HttpContext.Current).GetService(typeof(HttpWorkerRequest))).GetServerVariable( "CACHE_URL")
  • ((HttpWorkerRequest)((IServiceProvider)HttpContext.Current).GetService(typeof(HttpWorkerRequest))).GetServerVariable( "HTTP_URL")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

2024-07-24 09:04:02

编辑:您需要 HttpWorkerRequest.GetServerVariable() 与密钥 HTTP_URLCACHE_URL。 请注意,IIS 5 和 IIS 6 之间的行为有所不同(请参阅密钥文档)。

为了能够访问所有服务器变量(如果您得到 null),请直接访问 HttpWorkerRequest:

HttpWorkerRequest workerRequest = 
  (HttpWorkerRequest)((IServiceProvider)HttpContext.Current)
  .GetService(typeof(HttpWorkerRequest)); 

Edit: You want the HttpWorkerRequest.GetServerVariable() with the key HTTP_URL or CACHE_URL. Note that the behavior differs between IIS 5 and IIS 6 (see documentation of the keys).

In order to be able to access all server variables (in case you get null), directly access the HttpWorkerRequest:

HttpWorkerRequest workerRequest = 
  (HttpWorkerRequest)((IServiceProvider)HttpContext.Current)
  .GetService(typeof(HttpWorkerRequest)); 
千笙结 2024-07-24 09:04:02

还要记住,“用户输入的确切 URL”可能永远不会在服务器上可用。 从手指到服务器的链中的每个链接都可以稍微修改请求。

例如,如果我在浏览器窗口中输入 xheo.com,IE 将转换为 http://www.xheo.com< /a> 自动。 然后,当请求到达 IIS 时,它会对浏览器说 - 您确实想要位于 http:// 的默认页面www.xheo.com/Default.aspx。 因此浏览器会通过请求默认页面来响应。

HTTP 30x 重定向请求也会发生同样的情况。 服务器可能只会看到浏览器发出的最终请求。

Remember too that the "exact URL that the user entered" may never be available at the server. Each link in the chain from fingers to server can slightly modify the request.

For example if I type xheo.com into my browser window, IE will be convert to http://www.xheo.com automatically. Then when the request gets to IIS it says to the browser - you really want the default page at http://www.xheo.com/Default.aspx. So the browser responds by asking for the default page.

Same thing happens with HTTP 30x redirect requests. The server will likely only ever see the final request made by the browser.

|煩躁 2024-07-24 09:04:02

尝试使用Request.Url.OriginalString
可能会给你你正在寻找的东西。

Try using Request.Url.OriginalString
Might give you the thing you are looking for.

魂ガ小子 2024-07-24 09:04:02

有可能,您只需要组合请求对象中的一些值即可重建输入的确切网址:

Dim pageUrl As String = String.Format("{0}://{1}{2}", 
    Request.Url.Scheme, 
    Request.Url.Host, 
    Request.RawUrl)

Response.Write(pageUrl)

输入地址 http://yousite.com/?hello 准确返回:

http://yousite.com/?hello

It is possible, you just need to combining a few of the values from the request object to rebuild the exact url entered:

Dim pageUrl As String = String.Format("{0}://{1}{2}", 
    Request.Url.Scheme, 
    Request.Url.Host, 
    Request.RawUrl)

Response.Write(pageUrl)

Entering the address http://yousite.com/?hello returns exactly:

http://yousite.com/?hello
瞄了个咪的 2024-07-24 09:04:02
Request.RawUrl

我想你要找的就是那只猴子……

Request.RawUrl

I think is the monkey you are after...

合久必婚 2024-07-24 09:04:02

最简单的方法是使用客户端编程来提取准确的 url:

<script language="javascript" type="text/javascript"> 
document.write (document.location.href); 
</script>

Easiest way to do this is used client-side programming to extract the exact url:

<script language="javascript" type="text/javascript"> 
document.write (document.location.href); 
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文