如何在每次点击时向 ASP.NET 菜单项 url 添加随机数
防止缓存的一个步骤(除了添加适当的标头等之外)是在 URL 末尾添加一个随机数。
我正在使用 ASP.NET 菜单,并且希望在单击每个菜单项的导航 URL 时添加一个随机数。。
我可以在 MenuItemDataBound 事件中执行此操作,但对 MenuItemClicked 事件执行相同操作的运气不佳。
回答(8小时内无法回答我自己的问题,而且我没有时间等那么久,所以这是我的服务器端解决方案。)
要执行此服务器端操作,我必须从菜单中删除站点地图和数据绑定。
我只是将站点地图中的所有项目作为菜单项添加到菜单标记中的项目集合中,删除了 url 属性。这里的关键是删除 url 属性。
<asp:menu>
<items>
<asp:menuitem Text="Home" ToolTip="Go Home" Selectable="True" />
</items>
</asp:menu>
然后,在后面的代码中,您可以处理 MenuItemClicked 事件(现在应该触发该事件,因为标记中不再有 navigatorurl)。
在 MenuItemClicked 事件代码隐藏中,我只需执行以下操作:
string TimeStamp = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
// get iframe control - must have 'runat=server' attribute
HTMLControl display = CType(this.FindControl("display"), HTMLControl);
// dispatch menuitem
switch (e.item.valuepath)
{
case "Home":
display.attributes("src") = "home.aspx?=" + TimeStamp()
break;
.
.
.
}
这是带有 iframe 的服务器端解决方案。
One step to prevent caching (in addition to adding the appropriate headers etc..) is to add a random number to the end of my URLs.
I'm using an ASP.NET menu and would like to add a random number to each menu item's navigate URL as it is clicked.
I can do this in the MenuItemDataBound event, but haven't had much luck doing the same with the MenuItemClicked Event.
Answer (can't answer my own question for 8 hours, and I don't have time to wait that long so here's my server side solution.)
To do this server side, I've had to remove the sitemap and the databinding from the menu.
I simply added all of the items from the sitemap as menuitems to the items collection in the menu markup removing the url property. The key here is removing the url property.
<asp:menu>
<items>
<asp:menuitem Text="Home" ToolTip="Go Home" Selectable="True" />
</items>
</asp:menu>
Then in your code behind you can handle the MenuItemClicked event (which should now fire, because there is no longer a navigateurl in the markup).
In the MenuItemClicked event codebehind I simply do the following:
string TimeStamp = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
// get iframe control - must have 'runat=server' attribute
HTMLControl display = CType(this.FindControl("display"), HTMLControl);
// dispatch menuitem
switch (e.item.valuepath)
{
case "Home":
display.attributes("src") = "home.aspx?=" + TimeStamp()
break;
.
.
.
}
This is the server side solution with an iframe.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道您是否正在考虑将客户端 URL 操作作为一种选择,但是在每个页面加载时运行这一点 JavaScript 将会通过向每个链接附加时间戳来为您提供所需的行为。您可以将其修改为网站特定区域/div 中的目标链接,但此示例将全部更改:
由于每次页面加载时时间戳都会不同,因此您应该始终获得一组唯一的链接。
编辑这是一个工作的jsFiddle演示了行为:http://jsfiddle.net/2HzqU/ 2/
I don't know if you're considering client-side URL manipulation as an option, but running this little bit of JavaScript on each page load would give you the behavior you're looking for by appending a timestamp to each of the links. You can modify it to target links in a specific area/div of the site, but this example will change them all:
Since every time the page loads the timestamp will be different, you should always get a unique set of links.
EDIT Here's a working jsFiddle demoing the behavior: http://jsfiddle.net/2HzqU/2/
我认为这不是最好的解决方案。您是否尝试过使用这样的东西:
I don't think that's the best solution. Have you tried using something like this: