路由到另一个页面上的锚点

发布于 2024-09-07 00:13:19 字数 764 浏览 5 评论 0原文

我正在 ASP.NET 4 中使用 Web 表单路由,并且尝试路由到页面上的特定位置。在该页面上,我有一个类似

的元素,我想从另一个页面跳转到该锚点。为此,我在 global.asax 中定义了一个路由:

RouteTable.Routes.MapPageRoute("MyRoute", "Path/SubPath/{PageAnchor}",
    "~/MyPage.aspx", true, new RouteValueDictionary { { "PageAnchor", null } });

链接到该页面的超链接和锚点“3”在标记中以这种方式定义:

<asp:HyperLink ID="HyperLink1" runat="server"
    NavigateUrl="<%$ RouteUrl:RouteName=MyRoute,PageAnchor=#3 %>">
    Link</asp:HyperLink>

生成的链接的问题是 < URL 中的 code># 字符由 %23 编码,如下:http://localhost:1234/Path/SubPath/%233 这样我到达目标页面但未到达指定的锚点。

有没有办法避免这种不希望的 URL 编码?或者有其他方法可以路由到锚点吗?

先感谢您!

I am using Web Forms Routing in ASP.NET 4 and I am trying to route to a specific location on a page. On that page I have an element like <div id="3"> and I'd like to jump to this anchor from another page. For this purpose I have defined a Route in global.asax:

RouteTable.Routes.MapPageRoute("MyRoute", "Path/SubPath/{PageAnchor}",
    "~/MyPage.aspx", true, new RouteValueDictionary { { "PageAnchor", null } });

The HyperLink to link to that page and the anchor "3" is defined this way in markup:

<asp:HyperLink ID="HyperLink1" runat="server"
    NavigateUrl="<%$ RouteUrl:RouteName=MyRoute,PageAnchor=#3 %>">
    Link</asp:HyperLink>

The problem with the generated link is that the # character in the URL gets encoded by %23 this way: http://localhost:1234/Path/SubPath/%233 so that I reach the target page but not at the specified anchor.

Is there a way to avoid this unwished URL-encoding? Or any other way to route to an anchor?

Thank you in advance!

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

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

发布评论

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

评论(4

谎言 2024-09-14 00:13:19

ASP.NET 的路由功能不支持锚点。路由设计为仅支持应用程序路径之后、锚点之前的 URL 部分。

我建议添加一个事件处理程序(例如 Page_Load),并在该事件处理程序中生成 URL、附加锚点并在 HyperLink 控件上设置值。

当然,在大多数情况下,使用 Web 表单路由时,最简单的方法是手动将 URL 设置为您想要的任何内容。当 URL 不复杂且不太可能更改时,这是一个不错的选择。

Anchors are not supported with ASP.NET's routing feature. Routing is designed to support only the part of the URL after the application's path and before the anchor.

I suggest adding an event handler (e.g. Page_Load) and in that event handler generate the URL, append the anchor, and set the value on the HyperLink control.

Of course, in most cases with Web Forms routing it's easiest to just set the URL manually to whatever you want. This is a nice option when the URL is not complex and is unlikely to change.

墟烟 2024-09-14 00:13:19

这有效吗?

RouteTable.Routes.MapPageRoute("MyRoute", "Path/SubPath/#{PageAnchor}",
    "~/MyPage.aspx", true, new RouteValueDictionary { { "PageAnchor", null } })

<asp:HyperLink ID="HyperLink1" runat="server"
    NavigateUrl="<%$ RouteUrl:RouteName=MyRoute,PageAnchor=3 %>">
    Link</asp:HyperLink>

如果将 # 放在 PageAnchor 占位符之外,则可以避免该值被解码,而且这似乎是一种更简洁的方法。

Does this work?

RouteTable.Routes.MapPageRoute("MyRoute", "Path/SubPath/#{PageAnchor}",
    "~/MyPage.aspx", true, new RouteValueDictionary { { "PageAnchor", null } })

<asp:HyperLink ID="HyperLink1" runat="server"
    NavigateUrl="<%$ RouteUrl:RouteName=MyRoute,PageAnchor=3 %>">
    Link</asp:HyperLink>

If you place the # outside of the PageAnchor placeholder, you could avoid that value being decoded, and it seems like a cleaner way to do it, besides.

吲‖鸣 2024-09-14 00:13:19

路由转到控制器并使用锚参数重新路由到页面怎么样?

How about the route goes to a controller and that reroutes to the page with the anchor parameter?

Hello爱情风 2024-09-14 00:13:19

我知道这是一个老问题,但也许其他人可以从这种方法中受益。

创建没有锚点的路线。

RouteTable.Routes.MapPageRoute("MyRoute", "Path/SubPath", "~/MyPage.aspx");

然后像这样构建 url,附加锚点。

<a href="<%: GetRouteUrl("MyRoute", null) + "#3" %>">Link</a>

I know this is an old question, but maybe someone else could benefit from this approach.

Create a route without the anchor.

RouteTable.Routes.MapPageRoute("MyRoute", "Path/SubPath", "~/MyPage.aspx");

And then construct the url like this, appending the anchor.

<a href="<%: GetRouteUrl("MyRoute", null) + "#3" %>">Link</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文