ASP MVC 3 输出缓存未按预期运行
有人可以解释一下吗?操作以这种方式装饰:
[OutputCache(Duration = 5, VaryByParam = "none")]
public ActionResult MyAction()
{
// DO STUFF HERE...
}
MyAction 调用具有表单的视图(该表单回发到 MyAction POSTED-Method)。 (回发)机制运行良好。我声明持续时间 5,意思是 5 秒(或者不是?)。因此,5秒后,如果再次调用
http://myAddress/MyController/MyAction
。 。 。该动作未被调用!为什么?我缺少什么? 提前致谢!
Can someone please shed some light into this? An action is decorated this way:
[OutputCache(Duration = 5, VaryByParam = "none")]
public ActionResult MyAction()
{
// DO STUFF HERE...
}
The MyAction invokes a view which has a form (which posts back to the MyAction POSTED-Method).
The mechanism (of posting back) works fine. I declare Duration 5, meaning 5 seconds (or not?). So after 5 seconds if call it again
http://myAddress/MyController/MyAction
. . . the action is not called! Why? What am i missing?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试排除客户端缓存。 (清除浏览器中的缓存,或使用不同的浏览器,添加虚拟参数等)您的浏览器比非输出缓存页面更有可能缓存此内容,因为 MVC 实际上向客户端发送建议来缓存内容在指定的时间内。如果您的浏览器接受该建议并决定缓存比建议的时间更长的时间,我不会感到惊讶。
您的服务器端代码在什么时候再次触发? 20秒? 40?曾经?
Try to rule out client-side caching. (Clear the cache in your browser, or use a different browser, add a dummy parameter etc.) Your browser is more likely to cache this content than non-output-cached pages because MVC actually sends a suggestion to the client to cache the content for the specified time. I wouldn't be surprised if your browser took that suggestion and decided to cache longer than suggested.
At what point does your server-side code trigger again? 20 seconds? 40? ever?
如果你想测试 OutputCache 属性,这很容易做到:
控制器:
公共类 HomeController :控制器
{
[输出缓存(持续时间=60)]
公共 ActionResult Index()
{
ViewBag.Time = DateTime.Now;
}
查看
:
如果将其编译为“转到主页”操作,它将显示当前时间。如果刷新,则 60 秒内不会更新。
另一方面,我不确定您是否理解 VaryByParam 参数。这意味着它将缓存“none”查询参数的单独版本。
这意味着如果您有以下 URL:
它们都将彼此单独缓存。也许您确实理解这一点,但我将您的“无”解释为您不希望它因参数而变化,这就是为什么您将“无”放在那里。如果我误解了您的意图,请忽略这部分。
If you want to test the OutputCache attribute it is very easy to do so:
Controller:
public class HomeController : Controller
{
[OutputCache(Duration=60)]
public ActionResult Index()
{
ViewBag.Time = DateTime.Now;
}
}
View:
If you compile this an go to the home action, it will display the current time. If you refresh it, then it won't update for another 60 seconds.
On another note, I'm not sure you understand the VaryByParam parameter. This will mean that it will cache separate versions for the "none" query parameter.
That means if you have the following URLs:
They will all be cached separately from each other. Maybe you did understand this, but I was interpreting your "none" to mean that you didn't want it to vary by parameter and that's why you put "none" in there. If I misunderstood your intention then just ignore this part.
事实证明,这种行为是由操作方法内的错误逻辑造成的。 Firebug 中的调试显示以下内容:(
我认为)这是正确的行为!
谢谢大家的帮助!
Turns out that this behavior resulted from false logic inside the Action Method. Debugging in Firebug showed the following:
which (i think) is the correct behavior!
Thanks everybody for helping!