ASP.NET MVC 使用 Ajax 链接更新地址栏

发布于 2024-09-17 23:58:59 字数 380 浏览 12 评论 0原文

我正在尝试创建一个 ajax 驱动的图库,其中序列中的每张照片都使用 Ajax.Actionlink 加载。

用户可以通过向操作方法传递参数来获取任何给定的照片,例如:Gallery/Index?photo=100

问题在于,当用户使用Ajax 循环浏览照片时.Actionlink 的 URL 不再更新(正常回发期间的方式),因此他们无法从地址栏复制粘贴以返回照片。


我的问题是:在 ASP.NET MVC 中解决此问题的最佳方法是什么?我正在考虑的一件事是用主题标签更新地址栏,但坦率地说,我不知道这是否是一个好方法。

我可以使用一些最佳实践建议来解决这个问题。任何建议将不胜感激,谢谢。

I'm trying to create an ajax-driven gallery where each photo in a sequence is loaded with an Ajax.Actionlink.

The user can get to any given photo by passing a parameter to the action method, eg: Gallery/Index?photo=100

The problem is that when the user is cycling through photos with the Ajax.Actionlink's the URL is no longer being updated (the way it would be during normal post-backs) so they can't copy paste from the address bar to get back to a photo.


My question is: what is the best way to solve this issue in ASP.NET MVC? One thing I was thinking of was updating the address bar with hashtags, but frankly I don't know if this is a good approach.

I could use some best-practice advice on how to solve this problem. Any suggestions would be much appreciated, thank you.

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

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

发布评论

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

评论(2

留一抹残留的笑 2024-09-24 23:58:59

如果您确实想在每次 ajax 更新时更新地址栏,您可以使用几个 jquery / javascript 库,如本博客文章中所述:http://stephenwalther.com/archive/2010/04/08/jquery-asp-net-and-browser-history< /a>

然而,最佳实践解决方案通常是不要尝试伪造这样的东西(它只会出错)。如果您想让用户能够共享或链接到照片,请提供一个字段,其中包含适当的 url 或指向他们可以从中复制的 url 的永久链接。谷歌地图有一个很好的例子——如果你想与其他人分享地图。

If you really want to update the address bar with each ajax update there are a couple of jquery / javascript libraries you can use as described in this blog post: http://stephenwalther.com/archive/2010/04/08/jquery-asp-net-and-browser-history

However, the best practice solution is usually not to try and fake something like this (its only going to go wrong). If you want to give your users the ability to share or link to a photo is to provide a field with the appropriate url or permalink to the url that they can copy from. Google maps has a good example of this - if you wanted to share a map with someone else.

伊面 2024-09-24 23:58:59

我会考虑重写您的路线,以在路径中包含带照片的 ID。

例如,/Gallery/Index/100 而不是 ?photo=100。这就是为什么您的 ActionLink 方法无法正常工作,因为查询字符串不是路由的一部分。

I would look into rewriting your routes to include the photo ID in the path.

E.g., /Gallery/Index/100 instead of ?photo=100. This would be why your ActionLink methods aren't working how they should, as the querystring isn't part of the route.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文