MVC3的OutputCache没有服务器和客户机上按预期工作 [英] MVC3 OutputCache not working on Server and Client as expected

查看:152
本文介绍了MVC3的OutputCache没有服务器和客户机上按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在微软的MVC3框架使用的OutputCache属性有问题。

I'm having trouble using the OutputCache attribute in Microsoft's MVC3 framework.

请想象下面的控制器动作,它可以作为一个AJAX调用的一部分获得基于特定manufacturerId的产品列表:

Please imagine the following controller action, which can be used as part of an AJAX call to get a list of products based on a particular manufacturerId:

public JsonResult GetProducts(long manufacturerId)
{
    return Json(this.CreateProductList(manufacturerId), JsonRequestBehavior.AllowGet);
}

我想成为服务器,以避免过多的数据库查询缓存的这个结果。我可以这样配置属性实现这一点:

I want this result to be cached on the server to avoid making excessive database queries. I can achieve this by configuring the attribute thus:

[OutputCache(Duration = 3600, Location = OutputCacheLocation.Server, VaryByParam = "manufacturerId")]

这工作如我所料 - 浏览器发出这将导致服务器创建和缓存结果的初使请求时,来自相同或不同的浏览器后续请求得到缓存版本

This works as I expected - the browser makes an intitial request which causes the server to create and cache the result, subsequent requests from the same or different browser get the cached version.

不过......我也希望浏览器在本地缓存这些结果;如果我先过滤对制造商X,则Y然后返回到X,我不希望它作出X的产品的另一个请求 - 我希望它只是使用它的缓存的版本。

But... I also want the browser to cache these results locally; if I filter first on manufacturer X, then Y then go back to X, I don't want it to make another request for X's products - I want it to just use its cached version.

我可以做到这一点,通过改变的OutputCache这样:

I can make this happen, by changing the OutputCache to this:

[OutputCache(Duration = 3600, Location = OutputCacheLocation.Client)]

下面的问题:我如何结合这些,这样我可以有两套行为?我试着设置的位置ServerAndClient但这恰恰使得它的行为一样,当地点是服务器。

Here's the question: how do I combine these so that I can have both sets of behaviour? I tried setting the Location to ServerAndClient but this just made it behave the same as when Location was Server.

我敢肯定,这个问题有什么做的有所不同:*。响应头我ServerAndClient得到,但我不知道如何摆脱它。

I'm sure that the problem has something to do with the "Vary: *" response header I get with ServerAndClient but I don't know how to get rid of it.

我欢迎对我的意图的合理性意见 - 事实上,我没有得到我期望的结果让我觉得我可能有一些根本性的误解的地方...

I welcome comments about the rationality of my intentions - the fact that I'm not getting the results I expect makes me think I might have some fundamental misunderstanding somewhere...

非常感谢。

PS:这是在本地开发环境,从VS2010 IIS防爆preSS

PS: This is on a local dev environment, IIS Express from VS2010.

推荐答案

您可以使用OutputCacheLocation.Any它指定

You can use OutputCacheLocation.Any which specifies

的输出缓存可位于客户端浏览器上(其中
  请求起源),代理服务器(或任何其它服务器)上
  参与的请求,或在服务器上请求是在哪里
  处理。此值对应于HttpCacheability.Public
  枚举值。

The output cache can be located on the browser client (where the request originated), on a proxy server (or any other server) participating in the request, or on the server where the request was processed. This value corresponds to the HttpCacheability.Public enumeration value.

您可能要缓存控制公众对这些请求的HTTP头也被设置为

You may want to also set Cache-control public to in the HTTP header for these requests.

修改

看来,这取决于Web服务器的.NET版本,你可能需要包括 Response.Cache.SetOmitVaryStar(真); 在你的控制器行动,清除*因人而异头你的建议。

It seems, depending on your .Net version of the web server you may need to include Response.Cache.SetOmitVaryStar(true); within your controller action to remove the Vary * headers as you suggest.

为什么在.NET 4中重大更改发布说明<原因/详情一>

Details of the reason why in .Net 4 breaking changes release notes.

这篇关于MVC3的OutputCache没有服务器和客户机上按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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