0

In order to disable browser cache, to reliably instructing the browser not to cache, I found the best solution was to create your own [NoCache] attribute class

public class NoCacheSettingsAttribute : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
        filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        filterContext.HttpContext.Response.Cache.SetNoStore();

        base.OnResultExecuting(filterContext);
    }
}

And a global filter settings

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    // Makes sure that cached pages are not served to any browser including chrome
    filters.Add(new NoCacheSettingsAttribute());
}

I want to use [OutputCache(CacheProfile = "2Hour")] in some ActionResult of a controller and also want to use NoCacheSetting for rest of controller globally , Let say in BaseController, (all controller inherit from BaseController)

So the question is that will it work properly ? Or I have to put rest of controller 1 by one ?

1 Answer 1

1

Why not simply exclude controller actions already decorated with the OutputCache attribute from processing:

public class NoCacheSettingsAttribute : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        var descriptor = new ReflectedControllerDescriptor(
            filterContext.Controller.GetType()
        );

        var action = descriptor.FindAction(
            filterContext.Controller.ControllerContext, 
            filterContext.RequestContext.RouteData.GetRequiredString("action")
        );

        if (!action.GetCustomAttributes(typeof(OutputCacheAttribute), true).Any())
        {
            // The controller action is not decorated with the 
            // [OutputCache] attribute, so you could apply your NoCache logic here

            filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
            filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            filterContext.HttpContext.Response.Cache.SetNoStore();
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@Darni Dimitrov: Not required to put base.OnResultExecuting(filterContext); ?
No, completely unnecessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.