Asp.net mvc 使用Misc/ClearCache通过URL清除Asp.net MVC缓存

Asp.net mvc 使用Misc/ClearCache通过URL清除Asp.net MVC缓存,asp.net-mvc,caching,iis,Asp.net Mvc,Caching,Iis,有一种“技巧”我已经见过不止一次了,通过调用一个类似于的特定url来清除MVC应用程序的缓存 我用过几次,但老实说,我并不完全理解它是如何工作的,我也没有找到任何关于它的文档或帖子 有没有人可以解释一下,可能有一些相关的文档 非常感谢。我的评论错了,不需要Ajax 以下是您的JQuery: $("#clearCache").click(function () { if (!$(this).disabled) { $('<iframe id="cacheCleare

有一种“技巧”我已经见过不止一次了,通过调用一个类似于的特定url来清除MVC应用程序的缓存

我用过几次,但老实说,我并不完全理解它是如何工作的,我也没有找到任何关于它的文档或帖子

有没有人可以解释一下,可能有一些相关的文档


非常感谢。

我的评论错了,不需要Ajax

以下是您的JQuery:

$("#clearCache").click(function () {
    if (!$(this).disabled) {
        $('<iframe id="cacheCleared" src="../../Misc/ClearCache"></iframe>').appendTo('body').hide();
    }
});
一旦附加了
iframe
,它将尝试从该源加载内容。当它试图加载该源代码时,它将命中控制器中的
ClearCache
,并应清除所有内容

编辑:
返回视图()之前添加此项如果要避免为其生成视图

System.Web.HttpContext.Current.Response.End();

我不确定您是否使用了插件或第三方库。但这很简单:

  • 将web方法写入clearcache,如:

    public void Clearcache() 
    { 
    HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
            HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            HttpContext.Current.Response.Cache.SetNoStore(); 
    }
    
  • 使用类似控制器/actionname从浏览器调用方法:


  • 如果缓存是第三方的,您可以根据需要使用相同的方法删除它

    我想您可以在ajax请求中使用它,并将其加载到iframe中
    public void Clearcache() 
    { 
    HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
            HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            HttpContext.Current.Response.Cache.SetNoStore(); 
    }