获取用户在浏览器中输入的确切 url
我想获取用户在浏览器中输入的确切网址。 当然,我总是可以使用像 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
编辑:您需要
HttpWorkerRequest.GetServerVariable()
与密钥HTTP_URL
或CACHE_URL
。 请注意,IIS 5 和 IIS 6 之间的行为有所不同(请参阅密钥文档)。为了能够访问所有服务器变量(如果您得到
null
),请直接访问 HttpWorkerRequest:Edit: You want the
HttpWorkerRequest.GetServerVariable()
with the keyHTTP_URL
orCACHE_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:还要记住,“用户输入的确切 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.
尝试使用Request.Url.OriginalString
可能会给你你正在寻找的东西。
Try using
Request.Url.OriginalString
Might give you the thing you are looking for.
有可能,您只需要组合请求对象中的一些值即可重建输入的确切网址:
输入地址 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:
Entering the address http://yousite.com/?hello returns exactly:
我想你要找的就是那只猴子……
I think is the monkey you are after...
最简单的方法是使用客户端编程来提取准确的 url:
Easiest way to do this is used client-side programming to extract the exact url: