如果在生产中使用 MiniProfiler,这一点很重要。就像启用了 MiniProfiler 的用户首次访问页面一样,所有后续请求都将在 DOM 中包含 MiniProfiler 结果(因为它们现在已缓存)。结果不仅会不正确(因为他们只考虑首次加载),而且所有访问者都将能够看到您的 MiniProfiler 结果。
首先,为了启用甜甜圈洞缓存,我使用:
http://mvcdonutcaching.codeplex.com/ http://mvcdonutcaching.codeplex.com/
这允许您添加使用 OutputCache 时不会被缓存的操作。
鉴于上述情况,您可以删除@using StackExchange.Profiling;
从您的布局页面。然后您可以替换:
@MiniProfiler.RenderIncludes()
With:
@Html.Action("MiniProfiler", "DoNotCache", excludeFromParentCache: true)
我创建了一个 DoNotCache 控制器,因此所有不可缓存元素都在一起,但这不是必需的,您可以将此操作放置在任何控制器中。
public ActionResult MiniProfiler()
{
return View();
}
然后视图本身看起来就像:
@using StackExchange.Profiling;
@{
Layout = null;
}
@MiniProfiler.RenderIncludes()
这将确保 MiniProfiler 结果在适当的时候显示,并且即使在您使用 MiniProfiler 的地方也不会缓存在生产中DonutOutputCache
注解。