禁用所有操作的浏览器缓存,但保留其用于捆绑软件 [英] Disable browser cache for all actions, but keep it for bundles

查看:79
本文介绍了禁用所有操作的浏览器缓存,但保留其用于捆绑软件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在使用的MVC应用程序中,出于安全原因(为了防止用户注销后再返回历史记录),我们不得不阻止所有操作阻止浏览器缓存。我们使用此解决方案实现了这一目标。

In an MVC App that I'm working on, we had to block browser cache on all actions for security reasons (preventing user from going back in history after he has logged out). We achieved this using this solution.

但是,我们做到了希望允许浏览器缓存CSS和JS包。不幸的是,上述解决方案似乎阻止了所有资源的缓存。在本地计算机上,它甚至包括图像之类的静态文件,尽管在远程服务器上IIS会处理这些文件(而不是App本身),因此不必担心。无论如何,是否有某种方法可以调整此解决方案以允许捆绑包被浏览器缓存?

However, we do want to allow browsers to cache css and js bundles. Unfortunately the solution mentioned above appears to block caching of all resources. On local machine it even includes static files like images, though on remote server IIS handles those files (rather than the App itself) so that's one less thing to worry about. Anyway, is there some way to tweak this solution to allow bundles to be cached by browser?

我知道我可以使用并将其添加到所有操作(甚至更好的是所有控制器)中,或添加一个新的基本控制器(默认情况下具有此过滤器)并进行设置我所有的控制器都可以继承它,但是还有更好的选择(不涉及更改项目中无数文件的选择)吗?

I know that I could use a filter like this one and add it to all the actions (or even better, all controllers) or add a new base controller, that has this filter by default, and set all my controllers to inherit from it, but are there any better alternatives (ones that don't involve changing myriad files in the project)?

PS写了这个问题后,我想到了一些必须尝试的解决方案。这以前发生在我身上。我的意思是,在此处写问题时找到了正确的答案,但我最终没有发布这些问题。

P.S. Having written this question has made me think of a few solutions that I have to try. This happened to me before. I mean, finding the right answer while writing a question here, but I ended up not posting those questions.

解决方案在写这个问题时出现在我身上的确很简单。只需在 Application_BeginRequest 内编写一个简单的 if 条件,即可根据请求url确定资源是否可缓存。 ...我还没有测试过,但是听起来好像可以完成工作。

The solution which appeared to me while writing this question is really simple. Just write a simple if condition inside of Application_BeginRequest to determine if the resource should be cacheable or not based on the request url... I haven't tested it yet, but it sounds like it might just do the job.

推荐答案

我在原始问题中提到的解决方案。这很简单(有点脏),但似乎可以使用。

Here is the solution that I mentioned in the original question. It's very simple (and somewhat dirty), but it appears to work.

protected void Application_BeginRequest()
{
    // Here we check if the request was for bundle or not and depending on that
    // apply the cache block.
    if (!Request.Url.PathAndQuery.StartsWith("/bundles/"))
    {
        Response.Cache.SetAllowResponseInBrowserHistory(false);
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();
        Response.Cache.SetExpires(DateTime.Now);
        Response.Cache.SetValidUntilExpires(true);
    }
}

在我的本地环境中,我还添加了 / Content / 文件夹,但它在远程服务器上是多余的,因为IIS将处理这些(除非您明确地告诉它不要这样做)。

In my local envoriment I also added /Content/ folder to the condition, but it's redundant on the remote server, since IIS will handle those (unless you explicitly tell it not to).

这篇关于禁用所有操作的浏览器缓存,但保留其用于捆绑软件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆