Output caching for an ApiController (MVC4 Web API)

后端 未结 5 1307
我在风中等你
我在风中等你 2020-12-04 23:41

I\'m trying to cache the output of an ApiController method in Web API.

Here\'s the controller code:

public class TestController : ApiController
{
            


        
5条回答
  •  [愿得一人]
    2020-12-05 00:12

    You could use this on a regular MVC Controller:

    [OutputCache(Duration = 10, VaryByParam = "none", Location = OutputCacheLocation.Any)]
    public string Get()
    {
        HttpContext.Current.Response.Cache.SetOmitVaryStar(true);
        return System.DateTime.Now.ToString();
    }
    

    but OutputCache attribute is in System.Web.Mvc namespace and not available in an ApiController.

提交回复
热议问题