我正在明确提到禁用浏览器缓存页面所需的ASP.NET代码.有很多方法可以影响HTTP标头和元标记,我得到的印象是需要不同的设置才能使不同的浏览器正常运行.获得一个评论的参考位以表明哪些适用于所有浏览器以及哪些适用于特定浏览器(包括版本)是非常好的.
关于这个问题有大量的信息,但我还没有找到一个很好的参考资料来描述每种方法的好处,以及某种技术是否已被更高级别的API取代.
我对ASP.NET 3.5 SP1特别感兴趣,但同样可以获得早期版本的答案.
此博客文章Firefox和IE缓存之间的两个重要差异描述了一些HTTP协议行为差异.
以下示例代码说明了我感兴趣的内容
public abstract class NoCacheBasePage : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
DisableClientCaching();
}
private void DisableClientCaching()
{
// Do any of these result in META tags e.g. <META HTTP-EQUIV="Expire" CONTENT="-1">
// HTTP Headers or both?
// Does this only work for IE?
Response.Cache.SetCacheability(HttpCacheability.NoCache);
// Is this required for FireFox? Would be good to do this without magic strings.
// Won't it overwrite the previous setting
Response.Headers.Add("Cache-Control", "no-cache, no-store"); …Run Code Online (Sandbox Code Playgroud) 我的网站和IE有问题.我有一个文件Document.ashx,它根据查询字符串中传递的参数成为我数据库中的文档.
该文件适用于:
如果:
Windows Internet Explorer
无法从MyHostName下载Document.ashx.
无法打开此Internet站点.
请求的网站不可用或无法找到.
请稍后再试.
有没有人知道会导致这种情况的原因.当然它在Firefox中运行良好.
我在办公室里找了几个人用IE来试试,他们都遇到了同样的问题.他们都说它适用于Firefox.
我正在使用ASP.NET WebApi并使用以下代码来阻止所有内容中的缓存:
public override System.Threading.Tasks.Task<HttpResponseMessage> ExecuteAsync(System.Web.Http.Controllers.HttpControllerContext controllerContext, System.Threading.CancellationToken cancellationToken)
{
System.Threading.Tasks.Task<HttpResponseMessage> task = base.ExecuteAsync(controllerContext, cancellationToken);
task.GetAwaiter().OnCompleted(() =>
{
task.Result.Headers.CacheControl = new CacheControlHeaderValue()
{
NoCache = true,
NoStore = true,
MaxAge = new TimeSpan(0),
MustRevalidate = true
};
task.Result.Headers.Pragma.Add(new NameValueHeaderValue("no-cache"));
task.Result.Content.Headers.Expires = DateTimeOffset.MinValue;
});
return task;
}
Run Code Online (Sandbox Code Playgroud)
结果标题看起来像这样(chrome):
Cache-Control:no-store, must-revalidate, no-cache, max-age=0
Content-Length:1891
Content-Type:application/json; charset=utf-8
Date:Fri, 19 Jul 2013 20:40:23 GMT
Expires:Mon, 01 Jan 0001 00:00:00 GMT
Pragma:no-cache
Server:Microsoft-IIS/8.0
Run Code Online (Sandbox Code Playgroud)
在阅读了有关错误(如何阻止缓存中的chrome)之后,我添加了"no-store" .
但是,无论我做什么,当我做一些导航我远离此页面的东西,然后使用"后退"按钮时,chrome总是从缓存中加载:
Request Method:GET
Status Code:200 OK (from cache) …Run Code Online (Sandbox Code Playgroud) 要在控制器中设置没有缓存的响应,您可以执行以下操作:
$response = new Response();
$result = $this->renderView(
'AcmeDemoBundle:Default:index.html.twig',
array('products' => $products, 'form' => $form->createView()));
$response->headers->addCacheControlDirective('no-cache', true);
$response->headers->addCacheControlDirective('max-age', 0);
$response->headers->addCacheControlDirective('must-revalidate', true);
$response->headers->addCacheControlDirective('no-store', true);
$response->setContent($result);
return $response;
Run Code Online (Sandbox Code Playgroud)
但是使用注释,以确保每个方法都有相同的结果,你怎么办?
我试过但是继续保存缓存,如果我使用浏览器的后退按钮保留缓存:
/**
* @Cache(maxage="0", vary="no-cache, must-revalidate, no-store", smaxage="0", expires="now", public="false")
*/
class DefaultController extends Controller
{
/**
* Homepage: show products
*
* @Route("/", name="homepage")
* @Template
*/
public function indexAction()
{
$sessionCart = $this->get('demo');
$filters = $sessionCart->getFilters($this->getDoctrine()->getEntityManager());
$products = $this->getDoctrine()->getRepository('AcmeDemoBundle:Product')->search($filters);
$form = $this->createForm(new FilterType, $filters);
return array('products' => $products, 'form' => $form->createView()); …Run Code Online (Sandbox Code Playgroud) 在2007年的一篇名为" 使用Javascript掌握后退按钮 "的帖子中,一位名叫Patrick Hunlock的人声称只在页面中包含一个onbeforeunload处理程序就会停止缓存.(我猜浏览器缓存.)他非常强调它:
只是拥有一个unbeforeunload事件处理程序 - 无论它是否实际执行任何操作,无论是否生成对话框,即使整个函数声明完全由{}组成 - 只需定义一个事件处理程序将阻止页面被缓存 - 永远.
事实上,即使您允许页面缓存,该页面也不会被缓存.拥有onbeforeunload事件意味着每次访问页面时都会重新构建页面.Javascripts将重新运行,服务器端脚本将重新运行,页面将被构建,就好像用户第一次点击它一样,即使用户只是通过按下后退或前进按钮到达页面.
有趣的是,除了一两个提到Hunlock先生职位的人之外,我在其他任何地方都找不到任何提及.任何人都可以澄清这个问题吗?这是真的吗,还是我能安全地使用那个活动?
我这样做:
domain.com/route-name/?do-something=1
Run Code Online (Sandbox Code Playgroud)
..which设置一个cookie,然后使用302重定向重定向到这个:
domain.com/route-name/
Run Code Online (Sandbox Code Playgroud)
它允许在不管页面查看的情况下进行操作(cookie存储用户的设置).
我使用默认的Symfony2反向代理缓存,一切都很好,但我需要阻止上述请求缓存.
我正在使用它来执行重定向:
// $event being a listener, usually a request listener
$response = new RedirectResponse($url, 302);
$this->event->setResponse($response);
Run Code Online (Sandbox Code Playgroud)
我尝试过这样的事情,但似乎没有任何效果:
$response->setCache(array('max_age' => 0));
header("Cache-Control: no-cache");
Run Code Online (Sandbox Code Playgroud)
那么如何阻止它缓存这些页面呢?
caching ×3
http ×2
symfony ×2
asp.net ×1
browser ×1
download ×1
javascript ×1
rest ×1
symfony-2.3 ×1