Yasser Yasser - 1 year ago 117
C# Question

How to use caching in ASP.NET Web API?

I am using ASP.NET MVC 4 with WEB API

I have the following action, in the action shown below, my service method makes a db call to

method and returns an integer value which I am then using on every page, this action is called using an ajax call.

Below is my WEB API action :

[OutputCache(Duration = 86400, VaryByParam = "none")]
public int GetMyMagicNumber()
if (WebSecurity.IsAuthenticated)
var revenue = _magicService.DoMagic();
return revenue;
return 0;

My question : I haved tried using
[OutputCache(Duration = 86400, VaryByParam = "none")]
and I excepted that only the first time the db call will be made and next subsequent request to this action will return me the cached value, but this is not happening.

A db call is again made, the db call takes time how do I get this working ?

Answer Source

Unfortunately, caching is not built into ASP.NET Web API.

Check this out to get you on track: http://www.strathweb.com/2012/05/output-caching-in-asp-net-web-api/

An updated resource here: https://github.com/filipw/AspNetWebApi-OutputCache

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download