0

I'm trying to set some caching HTTP headers for all requests that return an "HTML page".

I could do that in some global place such as BeginRequest or in a global MVC filter (as suggested in that question).

For that to work I must differentiate HTML pages from other requests that are supposed to be cached (mostly resources I think but I'm not sure).

I am unsure how to define "HTML page" in a rigorous way so that I could put it into an algorithm. How could I detect such requests?

I really do not want to mark up all MVC actions that I write individually. That's tedious and I could forget something.

Community
  • 1
  • 1
boot4life
  • 4,292
  • 5
  • 20
  • 40

1 Answers1

0

I think the best way is with filters, then I'd use a regex to check if the result has HTML:

Create a filter:

public class NoCacheForHTMLResult : IActionFilter
{

    public void OnActionExecuted(ActionExecutedContext filterContext)
    {
        Regex tagRegex = new Regex(@"<[^>]+>");


        string response = filterContext.RequestContext.HttpContext.Response.Output.ToString();
        bool hasHtml = tagRegex.IsMatch(response);

        if (hasHtml)
        {
            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();
        }
    }

    public void OnActionExecuting(ActionExecutingContext filterContext)
    {

    }

}

...and add in the Global Filters (inside your App_Start):

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new NoCacheForHTMLResult());
    }
}

I'm not a regex expert, I got this regex from this question, but it looks we need to improve this, by checking if there is at least an HTML tag, otherwise if the result was XML, it looks the regex would match (though you might want to set no cache for XML as well, it's up to you).

The code for disabling cache I got in this answer, you might have a different code.

Hope this helps you.

Community
  • 1
  • 1
Alisson
  • 6,940
  • 3
  • 46
  • 65