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

时间:2022-08-14 03:51:13

How to disable automatic browser caching from asp.Net mvc application?

如何从asp中禁用自动浏览器缓存。净mvc应用程序?

Because I am having a problem with caching as it caches all links. But sometimes it redirected to DEFAULT INDEX PAGE automatically which stored it caching and then all the time I click to that link it will redirect me to DEFAULT INDEX PAGE.

因为我在缓存所有链接时遇到了问题。但有时它会自动重定向到默认索引页存储缓存,然后我一直点击链接,它会将我重定向到默认索引页。

So some one know how to manually disable caching option from ASP.NET MVC 4?

有些人知道如何从ASP中手动禁用缓存选项。净MVC 4 ?

5 个解决方案

#1


128  

You can use the OutputCacheAttribute to control server and/or browser caching for specific actions or all actions in a controller.

您可以使用OutputCacheAttribute来控制服务器和/或浏览器缓存,以缓存控制器中的特定动作或所有动作。

Disable for all actions in a controller

禁用控制器中的所有操作

[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
{
  // ... 
}

Disable for a specific action:

禁用特定动作:

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

If you want to apply a default caching strategy to all actions in all controllers, you can add a global action filter by editing your global.asax.cs and looking for the RegisterGlobalFilters method. This method is added in the default MVC application project template.

如果您想对所有控制器中的所有操作应用默认缓存策略,您可以通过编辑global.asax添加一个全局操作过滤器。寻找RegisterGlobalFilters方法。此方法添加到默认的MVC应用程序项目模板中。

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

This will cause it to apply the OutputCacheAttribute specified above to every action, which will disable server and browser caching. You should still be able to override this no-cache by adding OutputCacheAttribute to specific actions and controllers.

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

#2


28  

HackedByChinese is missing the point. He mistook server cache with client cache. OutputCacheAttribute controls server cache (IIS http.sys cache), not browsers (clients) cache.

哈克德比中文没有抓住要点。他把服务器缓存和客户端缓存搞错了。OutputCacheAttribute控制服务器缓存(IIS http)。系统缓存),而不是浏览器(客户端)缓存。

I give you a very small part of my codebase. Use it wisely.

我给了你一小部分代码基。明智地使用它。

[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);
    }
}

Usage:

用法:

/// will be applied to all actions in MyController
[NoCache]
public class MyController : Controller
{
    // ... 
}

Use it wisely as it really disables all client cache. The only cache not disabled is the "back button" browser cache. But is seems there is really no way to get around it. Maybe only by using javascript to detect it and force page or page zones refresh.

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

#3


12  

We can set cache profile in the Web.config file instead of setting cache values individually in pages to avoid redundant code. We can refer the profile by using the CacheProfile property of the OutputCache attribute. This cache profile will be applied to all pages unless the page/method overrides these settings.

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

<system.web>
  <caching>
    <outputCacheSettings>
      <outputCacheProfiles>
        <add name="CacheProfile" duration="60" varyByParam="*" />
      </outputCacheProfiles>
    </outputCacheSettings>
  </caching>
</system.web>

And if you want to disable the caching from your particular action or controller, you can override the config cache settings by decorating that specific action method like shown below:

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

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

Hope this is clear and is useful to you.

希望这是清楚的,对你有用。

#4


9  

If you want to prevent browser caching, you can use this code from ShareFunction

如果您想防止浏览器缓存,可以使用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);
}

#5


5  

For on page solution ,Set this in your layout page :

在页面解决方案中,请将此设置在您的布局页面中:

<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">

#1


128  

You can use the OutputCacheAttribute to control server and/or browser caching for specific actions or all actions in a controller.

您可以使用OutputCacheAttribute来控制服务器和/或浏览器缓存,以缓存控制器中的特定动作或所有动作。

Disable for all actions in a controller

禁用控制器中的所有操作

[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
{
  // ... 
}

Disable for a specific action:

禁用特定动作:

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

If you want to apply a default caching strategy to all actions in all controllers, you can add a global action filter by editing your global.asax.cs and looking for the RegisterGlobalFilters method. This method is added in the default MVC application project template.

如果您想对所有控制器中的所有操作应用默认缓存策略,您可以通过编辑global.asax添加一个全局操作过滤器。寻找RegisterGlobalFilters方法。此方法添加到默认的MVC应用程序项目模板中。

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

This will cause it to apply the OutputCacheAttribute specified above to every action, which will disable server and browser caching. You should still be able to override this no-cache by adding OutputCacheAttribute to specific actions and controllers.

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

#2


28  

HackedByChinese is missing the point. He mistook server cache with client cache. OutputCacheAttribute controls server cache (IIS http.sys cache), not browsers (clients) cache.

哈克德比中文没有抓住要点。他把服务器缓存和客户端缓存搞错了。OutputCacheAttribute控制服务器缓存(IIS http)。系统缓存),而不是浏览器(客户端)缓存。

I give you a very small part of my codebase. Use it wisely.

我给了你一小部分代码基。明智地使用它。

[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);
    }
}

Usage:

用法:

/// will be applied to all actions in MyController
[NoCache]
public class MyController : Controller
{
    // ... 
}

Use it wisely as it really disables all client cache. The only cache not disabled is the "back button" browser cache. But is seems there is really no way to get around it. Maybe only by using javascript to detect it and force page or page zones refresh.

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

#3


12  

We can set cache profile in the Web.config file instead of setting cache values individually in pages to avoid redundant code. We can refer the profile by using the CacheProfile property of the OutputCache attribute. This cache profile will be applied to all pages unless the page/method overrides these settings.

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

<system.web>
  <caching>
    <outputCacheSettings>
      <outputCacheProfiles>
        <add name="CacheProfile" duration="60" varyByParam="*" />
      </outputCacheProfiles>
    </outputCacheSettings>
  </caching>
</system.web>

And if you want to disable the caching from your particular action or controller, you can override the config cache settings by decorating that specific action method like shown below:

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

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

Hope this is clear and is useful to you.

希望这是清楚的,对你有用。

#4


9  

If you want to prevent browser caching, you can use this code from ShareFunction

如果您想防止浏览器缓存,可以使用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);
}

#5


5  

For on page solution ,Set this in your layout page :

在页面解决方案中,请将此设置在您的布局页面中:

<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">