我复制了您的代码并在我的机器上对其进行了测试,并将 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 http://localhost:52121/LabOne/MvcCache/About/admin/010。你会看到OutputCache正在工作,MVC会从缓存中取出结果,因为OutputCache之前已经缓存了这个URL。
更新
根据这个问题Using outputcache in MVC 及其接受的答案,缓存正在使用 URL,并且与 MVC 路由系统无关。