如何最有效地工作在ASP.NET MVC Expires头? [英] How best to work with the Expires header in ASP.NET MVC?

查看:207
本文介绍了如何最有效地工作在ASP.NET MVC Expires头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够设置一个长的到期时间,某些项目,它通过GET请求用户下载。

我想说这是很好的10分钟'(即我要为10分钟一个Expires头)。该请求是被显示在通过AJAX页面的HTML片段和他们是很好的为用户的会话。 我不想回去到服务器,并获得304 如果他们再需要他们 - 我想浏览器缓存立即给我相同的项目

我发现了一篇文章这几乎是一岁左右的MVC行动过滤器缓存和COM pression 。这将创建一个自定义ActionFilter改变expires头。我已经使用COM pression过滤器,对于一些自定义CSS我生成(94%玉米pression率!)的伟大工程。

我有两个主要关注点:

1)难道我真的要使用此方法。我很好,如果我这样做,但真的在MVC或无功能的OutputCache功能为我做到这一点?在传统ASP.NET我一直只是手动设置Expires头,但我们不能再这样了 - 至少在控制器

2)如果我使用此过滤器的方法 - 它会用的OutputCache政策,不惜一切干涉 - 我希望能够在web.config中控制。我有点想这两者是相互排斥的,你不会想都 - 但我不能完全确定。


解决方案

  1. 没有,你不这样做的有无的使用此方法。不过,我认为它可能是选择最好的方法,因为它使控制更容易测试和更少的网络感知。另一种方法是手动设置头中的控制器,如:

    Response.AddHeader(过期,星期四,1994 GMT十六点00分00秒12月1日);


  2. 那么,的OutputCache属性控制的时候,动作运行在所有的,当它而是返回HTML缓存。过期告诉浏览器何时重新读取HTML。所以,我不会把它们相互排斥的,但他们肯定是一个硬币的两面,你​​是对的认为你的可能的不是两者都需要。我建议审查 HTTP规范来决定什么是最适合您应用程序。


I want to be able to set a long expires time for certain items that a user downloads via GET request.

I want to say 'this is good for 10 minutes' (i.e. I want to set an Expires header for +10 minutes). The requests are fragments of HTML that are being displayed in the page via AJAX and they're good for the user's session. I don't want to go back to the server and get a 304 if they need them again - I want the browser cache to instantly give me the same item.

I found an article which is almost a year old about MVC Action filter caching and compression. This creates a custom ActionFilter to change the expires header. I'm already using the compression filter which works great for some custom css I am generating (94% compression rate!).

I have two main concerns :

1) Do I really have to use this method. I'm fine with it if I do, but is there really no functionality in MVC or the OutputCache functionality to do this for me? In 'traditional' ASP.NET I've always just set the Expires header manually, but we cant do that anymore - at least not in the controller.

2) If I do use this filter method - is it going to interfere with the OutputCache policy at all - which I want to be able to control in web.config. I'm kind of thinking the two are mutually exclusive and you wouldn't want both - but I'm not completely sure.

解决方案

  1. No, you don't have to use this method. However, I think it is probably the best method to choose, because it makes the controller more testable and less web-aware. The alternative would be to set the header manually in the Controller, like this:

    Response.AddHeader("Expires", "Thu, 01 Dec 1994 16:00:00 GMT");

  2. Well, the OutputCache attribute controls when the action runs at all, and when it returns cached HTML instead. Expires tells the browser when to re-fetch the HTML. So I wouldn't call them mutually exclusive, but they are certainly two sides of the same coin, and you're right to think that you may not need both. I would suggest reviewing the HTTP spec to decide what is most appropriate for your application.

这篇关于如何最有效地工作在ASP.NET MVC Expires头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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