Asp.net mvc 4 asp.net mvc4 VaryByParam不工作
我的代码如下:Asp.net mvc 4 asp.net mvc4 VaryByParam不工作,asp.net-mvc-4,varybyparam,Asp.net Mvc 4,Varybyparam,我的代码如下: [HttpGet] [OutputCache(Duration = 90, VaryByParam = "cityCode")] public ActionResult About(string userName, string cityCode) { //do something... return View(); } 当我访问URL时,缓存工作正常: 但是当我访问下面这个路由URL时,缓存不工作,为什么 我复制了您的代码,并在我的机器上对其进行了测试,
[HttpGet]
[OutputCache(Duration = 90, VaryByParam = "cityCode")]
public ActionResult About(string userName, string cityCode)
{
//do something...
return View();
}
我复制了您的代码,并在我的机器上对其进行了测试,我将RouteConfig配置如下
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "aboutRoute",
url: "{controller}/{action}/{userName}/{cityCode}",
defaults: new { controller = "Home", action = "About", userName = UrlParameter.Optional, cityCode = UrlParameter.Optional }
);
}
}
我也面临同样的问题,我将解释如下:
OutputCache
取决于URL,您提供的示例实际上是两个不同的URL,尽管它们会产生相同的结果
因此,尝试再次请求URL。您将看到,OutputCache
正在工作,MVC将从缓存中获取结果,因为OutputCache
在上一次缓存到这个URL
更新
根据这个问题及其公认的答案,缓存与URL一起工作,与MVC路由系统无关。请将路由配置放在此处