【问题标题】:Caching Asp .Net Web Api缓存 Asp .Net Web Api
【发布时间】:2014-07-18 08:31:52
【问题描述】:

我试图在我正在实现的 api 中捕获一个方法。我创建了一个基于本文的属性 (http://www.codeproject.com/Articles/682296/Setting-Cache-Control-HTTP-Headers-in-Web-API-Cont)。

[Route("")]
[CacheControl(MaxAge = 160)]
public IEnumerable<Club> GetAll()
{
    return _clubService.GetAll();
}

这个属性的代码是:

public class CacheControlAttribute : ActionFilterAttribute
{
    public int MaxAge { get; set; }

    public CacheControlAttribute()
    {
        MaxAge = 160;
    }

    public override void OnActionExecuted(HttpActionExecutedContext context)
    {
        context.Response.Headers.CacheControl = new CacheControlHeaderValue
        {
            Public = true,
            MaxAge = TimeSpan.FromSeconds(MaxAge)
        }; 

        base.OnActionExecuted(context);
    }
}

我直接从 chrome 调用这个方法。但服务器始终在执行查询,我无法让服务器返回未修改。我做错了什么?

----------------------------------------------- - - - - - - - - 编辑 - - - - - - - - - - - - - - - - - ----------------------------------------- 这些是我的电话标题:

【问题讨论】:

  • 您是否检查了标头以查看 Web Api 返回的内容?
  • @DavidG 我已经添加了标题的捕获。
  • 我注意到 Web API 中的输出缓存与设置标头一样简单,有人已经冒昧地创建了一个支持它的library
  • @jvrdelafuente 您的浏览器正在发送cache-control: max-age=0,这实际上是在告诉服务器My data is stale, give me new stuff
  • @James 链接的库看起来功能很齐全,我会先尝试一下。

标签: c# caching asp.net-web-api asp.net-web-api2


【解决方案1】:

Web API 目前不支持输出缓存,但是,已经有人费心构建一个完全符合您需要的库 - AspNetWebApi-OutputCache

【讨论】:

    猜你喜欢
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    • 2012-08-07
    • 2013-01-16
    • 2018-07-26
    相关资源
    最近更新 更多