3

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?

1 Answer 1

2

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).

7
  • Thanks @Oleg, I have tested... but not works... the controller breakpoint is fired everytime :(
    – chemitaxis
    Feb 1, 2016 at 11:50
  • I have read it in this article: Response caching does not cache responses on the web server. It differs from output caching, which would cache responses in memory on the server in earlier versions of ASP.NET and ASP.NET MVC. Output caching middleware is planned to be added to ASP.NET MVC 6 in a future release.
    – chemitaxis
    Feb 1, 2016 at 11:51
  • My last comment, is the correct Answer, please edit and I will validate as good ;)
    – chemitaxis
    Feb 1, 2016 at 11:52
  • 1
    @chemitaxis: I tested [ResponseCache(Duration=60)] just now on one my test project. The breakpoint is fired once if I change the URL to another page and go to the URL of the action. Only one min later the breakpoints is fired next time. Of cause if I press F5 to force reloading then the breakpoint is fired immediately.
    – Oleg
    Feb 1, 2016 at 11:58
  • @chemitaxis: If you need memory caching, then it's absolutely another scenario. You can read the article about it. In my opinion HTTP caching (on the client side) is the best choice in the most scenarios because the data will be get from the local browser cache (or from the local proxy) without any communication with the web server.
    – Oleg
    Feb 1, 2016 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.