Can a view or partial view determine if it is cached or not?

My one action has the following attributes:

  [OutputCache(Location = System.Web.UI.OutputCacheLocation.Server, Duration = 300, VaryByParam = "*")]

and the other with

  [OutputCache(Location = System.Web.UI.OutputCacheLocation.None)]

and both of them use the same kind.

In a view or even in an action method, how do you determine what caching is? is it a cached page or not? I tried looking at Response.Headers (only has “Server: Microsoft-IIS / 7.0), and Response.CacheControl is“ private ”in both cases.

+3
source share
1 answer

, , , , ... Reflection 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;
            }
        }
    }
}
0

All Articles