ASP.NET MVC如何禁用自动缓存选项?

Raj*_*ala 71 asp.net caching c#-4.0 asp.net-mvc-4

如何禁用asp.Net mvc应用程序的自动浏览器缓存?

因为我在缓存所有链接时遇到缓存问题.但有时它会自动重定向到DEFAULT INDEX PAGE,并将其存储在缓存中,然后我一直点击该链接,它会将我重定向到DEFAULT INDEX PAGE.

所以有人知道如何从ASP.NET MVC 4手动禁用缓存选项?

Hac*_*ese 134

您可以使用OutputCacheAttribute控制服务器和/或浏览器缓存来执行控制器中的特定操作或所有操作.

禁用控制器中的所有操作

[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] // will be applied to all actions in MyController, unless those actions override with their own decoration
public class MyController : Controller
{
  // ... 
}
Run Code Online (Sandbox Code Playgroud)

禁用特定操作:

public class MyController : Controller
{
    [OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] // will disable caching for Index only
    public ActionResult Index()
    {
       return View();
    }
} 
Run Code Online (Sandbox Code Playgroud)

如果要对所有控制器中的所有操作应用默认缓存策略,可以通过编辑和查找方法来添加全局操作筛选器.此方法添加在默认的MVC应用程序项目模板中.global.asax.csRegisterGlobalFilters

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new OutputCacheAttribute
                    {
                        VaryByParam = "*",
                        Duration = 0,
                        NoStore = true,
                    });
    // the rest of your global filters here
}
Run Code Online (Sandbox Code Playgroud)

这将导致它将OutputCacheAttribute上面指定的内容应用于每个操作,这将禁用服务器和浏览器缓存.您仍然可以通过添加OutputCacheAttribute到特定操作和控制器来覆盖此无缓存.

  • 我认为它有效,但后来我开始得到持续时间必须是我的控制器中的正数. (2认同)
  • 这不能与ChildActions一起使用 (2认同)

Sof*_*ion 28

HackedByChinese缺少重点.他将服务器缓存误认为是客户端缓存.OutputCacheAttribute控制服务器缓存(IIS http.sys缓存),而不是浏览器(客户端)缓存.

我给你一小部分代码库.明智地使用它.

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public sealed class NoCacheAttribute : FilterAttribute, IResultFilter
{
    public void OnResultExecuting(ResultExecutingContext filterContext)
    {
    }

    public void OnResultExecuted(ResultExecutedContext filterContext)
    {
        var cache = filterContext.HttpContext.Response.Cache;
        cache.SetCacheability(HttpCacheability.NoCache);
        cache.SetRevalidation(HttpCacheRevalidation.ProxyCaches);
        cache.SetExpires(DateTime.Now.AddYears(-5));
        cache.AppendCacheExtension("private");
        cache.AppendCacheExtension("no-cache=Set-Cookie");
        cache.SetProxyMaxAge(TimeSpan.Zero);
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

/// will be applied to all actions in MyController
[NoCache]
public class MyController : Controller
{
    // ... 
}
Run Code Online (Sandbox Code Playgroud)

明智地使用它,因为它真的禁用所有客户端缓存.唯一未禁用的缓存是"后退按钮"浏览器缓存.但似乎真的没有办法绕过它.也许只能通过使用javascript来检测它并强制页面或页面区域刷新.

  • 我理解这一点.当使用OutputCacheAttribute并设置NoStore = true时,禁止浏览器缓存(响应头看起来像`Cache-Control:public,no-store,max-age = 0 Expires:Mon,22 Oct 2012 20:19:26 GMT最后修改时间:2012年10月22日星期一20:19:26 GMT`).因此,它将阻止服务器和浏览器缓存.我知道很奇怪除了没有商店外它还设置了`public`,但净效应是无存储和即时到期. (12认同)

Swa*_*pta 13

我们可以在Web.config文件中设置缓存配置文件,而不是在页面中单独设置缓存值以避免冗余代码.我们可以使用OutputCache属性的CacheProfile属性来引用配置文件.除非页面/方法覆盖这些设置,否则此缓存配置文件将应用于所有页面.

<system.web>
  <caching>
    <outputCacheSettings>
      <outputCacheProfiles>
        <add name="CacheProfile" duration="60" varyByParam="*" />
      </outputCacheProfiles>
    </outputCacheSettings>
  </caching>
</system.web>
Run Code Online (Sandbox Code Playgroud)

如果要禁用特定操作或控制器的缓存,可以通过修改特定的操作方法来覆盖配置缓存设置,如下所示:

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult NoCachingRequired()
{
    return PartialView("abcd");
}
Run Code Online (Sandbox Code Playgroud)

希望这很清楚,对你有用.


小智 9

如果要阻止浏览器缓存,可以使用ShareFunction中的此代码

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);
}
Run Code Online (Sandbox Code Playgroud)


Pra*_*bhe 5

对于页面解决方案,请在布局页面中设置:

<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
Run Code Online (Sandbox Code Playgroud)

  • 您可以添加无商店以使其在Chrome中运行。&lt;meta http-equiv =“ Cache-Control” content =“非缓存,不存储,必须重新验证”&gt;” (2认同)