23

我有一个这样的网址:

http://localhost/place/663828/bangkok-paradise-restaurant-toronto#r306040

我试图查看是否存在锚标记以及获取其值以在后面的代码中执行一些代码逻辑。

我一直在尝试使用 Page.Request,但没有一个属性显示 URL 的锚链接部分。

例如:

Response.Write(this.Page.Request.RawUrl.ToString());

我几乎尝试了此页面上的组合/属性:http ://www.west-wind.com/weblog/posts/269.aspx

只是为了完成这个话题:

我用永久链接复制了 Stack Overflow 的方法...:D

4

3 回答 3

29

在 ASP.NET 中无法从服务器端检索 #anchor。

这是一个客户端标志,告诉浏览器移动到页面中的特定位置。

您可以在正文onLoad事件中使用一些 JavaScript 代码来检查锚点并使用Ajax将其发送回服务器。

var anchorValue;
var url = document.location;
var strippedUrl = url.toString().split("#");
if(strippedUrl.Length > 1)
    anchorvalue = strippedUrl[1];

参考:从 URL 中检索锚值

于 2009-04-21T19:19:17.083 回答
16

更明确地说,任何浏览器都不会将锚标记作为 HTTP 请求的一部分发送。它仅在浏览器中本地解释。ASP.NET 或任何其他 Web 服务器技术、Microsoft 或其他方式都不会看到该请求的锚点。

RFC 1808 第 2.4.1 节 - “请注意,片段标识符不被视为 URL 的一部分。”

正如其他人所建议的那样,您可以获得的最接近的方法是使用客户端来读取浏览器窗口位置。

于 2009-04-21T19:38:33.200 回答
9

可以通过以下方式从 C# 中的 URL 解析片段:

var uri = new Uri("http://localhost?id=2#token=23");
var fragment = uri.Fragment; // Will return #token=23

There is a problem however in that the browser won't send fragments to the server. If you receive requests from a service that includes this information in the request, it will be available from the server side too.

于 2012-05-14T12:23:51.663 回答