asp.netasp.net-mvcasp.net-coreasp.net-core-mvcasp.net-caching

Caching in ASP.NET 5 in Controller


I am trying to cache a output controller like I did in ASP.NET MVC 5.

I did that in ASP.NET MVC 5 Controller:

 [OutputCache(Duration = 60, VaryByParam = "*", Location = OutputCacheLocation.ServerAndClient)]

Now, I'm trying that in ASP.NET 5 MVC 6:

Controller Attribute:

[ResponseCache(CacheProfileName = "TestCache")]

In my Startup.cs:

//Caching
            services.Configure<MvcOptions>(options => options.CacheProfiles.Add("TestCache", new CacheProfile()
            {
                Duration = 3600,
                Location = ResponseCacheLocation.Any,
                VaryByHeader = "*"
            }));

I have added a breakpoint in my TestController, but the breakboint is fired everytime.

How can I fix it?


Solution

  • You should use new attributes of MVC Actions described here. For example

    [ResponseCache(Duration=60)]
    

    corresponds to

    [OutputCache(Duration = 60)]
    

    It places HTTP header

    Cache-Control: public,max-age=60
    

    in the corresponding HTTP response.

    If you prefer to use caching profiles, you will find the corresponding information about the usage in the same article (see here).