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?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
chemitaxis
  • 13,889
  • 17
  • 74
  • 125

1 Answers1

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

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks @Oleg, I have tested... but not works... the controller breakpoint is fired everytime :( – chemitaxis Feb 01 '16 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 01 '16 at 11:51
  • My last comment, is the correct Answer, please edit and I will validate as good ;) – chemitaxis Feb 01 '16 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 01 '16 at 11:58
  • @chemitaxis: If you need memory caching, then it's absolutely another scenario. You can read [the article](https://docs.asp.net/en/latest/fundamentals/caching.html) 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 01 '16 at 12:03
  • Ok, I have pressed F5 every time... Thanks :) I need to check when Cache Server will be avalaible... – chemitaxis Feb 01 '16 at 12:05
  • @chemitaxis: You are welcome! I didn't tested myself, because I don't need it, but it seems that In Memory Caching is already implemented in RC1 (see my previous comment) – Oleg Feb 01 '16 at 12:07