【发布时间】:2011-12-10 17:10:24
【问题描述】:
我是 MVC 初学者,今天遇到了一个奇怪的问题:我想使用 OutputCache 来启用一个 action 的缓存。代码如下:
[OutputCache(Duration=86400,VaryByParam="none")]
public ActionResult Index(string id)
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
ViewBag.ID = id;
return View();
}
注意“VaryByParam”属性是“none”,是的,我希望服务器只为操作保留一个缓存,无论传递的参数是什么。 路由代码是这样的:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
然后我打开探索,结果不是我想要的,例如我输入:“http://localhost:27654/home/index/121212”,页面出来并显示id“121212”。但是当我更改为“http://localhost:27654/home/index/12”时,我看到页面已更改,显示 id“12”。
但是如果我刷新页面(参数“id”没有改变),页面中显示的日期时间没有改变,这意味着asp.net已经通过“ID”参数保持缓存变化,而不是我的设置。 怎么了?
【问题讨论】:
标签: asp.net asp.net-mvc caching outputcache