Configure the MVC.NET OutputCache to return 304 Not Modified if an ActionResult hasn't changed

假如想象 提交于 2019-12-05 23:30:56

问题


Introduce the Problem

We have successfully configured the browser cache to return a saved response if the server indicates 304 Not Modified. Here is the config:

<caching>
  <outputCacheSettings>
    <outputCacheProfiles>
      <add
        name="TransparentClient"
        location="Client"
        duration="0" />
    </outputCacheProfiles>
  </outputCacheSettings>
</caching>

The web.config is perfect and sets Cache-control:private, max-age=0 so that:

  1. The browser will cache responses,
  2. will always validate the cache, and
  3. will return the cached response if the server responds 304.

The problem is that our MVC.NET Actions always respond 200 and never 304.

The Problem

How do we configure output caching to return a 304 Not Modified when an ActionResult hasn't changed?

  • Is there any built-in cache validation in MVC.NET?
  • If it isn't, how do we roll-our-own cache validation mechanism?

The roll-our-own will probably require an Action Filter with ETag or Last-Modified.

Screen Shots

Here is a Fiddler screenshot showing the lack of a 304.

  • 318 is a SHIFT+Refresh.

  • 332 is a Refresh that we expected would result in a 304. Problem.

Search and Research

ASP.NET MVC : how do I return 304 "Not Modified" status? mentions returning a 304 from within the Action. That does not provide a way to make the OutputCache accurately respond with a 304.

Working with the Output Cache and other Action Filters shows how to override the OnResultExecuted, which will allow adding/removing headers.


回答1:


The following is working for us.

Web.Config

Set Cache-Control:private,max-age-0 to enable caching and force re-validation.

<system.web>
  <caching>
    <outputCacheSettings>
      <outputCacheProfiles>
        <add name="TransparentClient" duration="0" location="Client" />
      </outputCacheProfiles>
    </outputCacheSettings>
  </caching>
</system.web>

Action

Respond with 304 if the response is not modified.

[MyOutputCache(CacheProfile="TransparentClient")]
public ActionResult ValidateMe()
{
    // check whether the response is modified
    // replace this with some ETag or Last-Modified comparison
    bool isModified = DateTime.Now.Second < 30;

    if (isModified)
    {
        return View();
    }
    else
    {
        return new HttpStatusCodeResult(304, "Not Modified");
    }
}

Filter

Remove the Cache-Control:private,max-age-0 otherwise the cache will store the status message.

public class MyOutputCache : OutputCacheAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        base.OnResultExecuted(filterContext);
        if (filterContext.HttpContext.Response.StatusCode == 304)
        {
            // do not cache the 304 response                
            filterContext.HttpContext.Response.CacheControl = "";                
        }
    }
}

Fidder

Fiddler shows that the cache is acting appropriately.



来源:https://stackoverflow.com/questions/23775040/configure-the-mvc-net-outputcache-to-return-304-not-modified-if-an-actionresult

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!