我有一个 Action 具有这些属性:
[OutputCache(Location = System.Web.UI.OutputCacheLocation.Server, Duration = 300, VaryByParam = "*")]
还有一个
[OutputCache(Location = System.Web.UI.OutputCacheLocation.None)]
并且他们都使用相同的 View 。
在 View 中,甚至在操作方法中,我如何确定缓存是什么?即它是否是缓存页面?我试过查看 Response.Headers(只有“服务器:Microsoft-IIS/7.0”),& Response.CacheControl 在这两种情况下都是“私有(private)的”。
最佳答案
我没有在应用程序中使用它,但为了看看它是否可行,我已经完成了这个示例......使用反射和基本 Controller ,并从我的操作方法中调用 Initialize(),以下代码从调用方法获取 OutputCache 属性。然后 View 或 Partial View 可以从 ViewBag 中获取缓存位置。
public class BaseController : Controller
{
public void Initialize()
{
var stackTrace = new StackTrace();
if (stackTrace.FrameCount >= 1)
{
var methodBase = stackTrace.GetFrame(1).GetMethod();
var filters = (OutputCacheAttribute[])methodBase.GetCustomAttributes(typeof(OutputCacheAttribute), false);
if (filters.Length > 0)
{
ViewBag.CacheLocation = filters[0].Location;
}
}
}
}
关于asp.net-mvc - View 或 Partial View 是否可以确定它是否被缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5986707/