Thursday, February 2, 2017

Angular 2 , Web API Core & Entity Framework Core Disable Cache / json Caching

Scenario :
We encounter problem that when we edit entity and try to view its details or edit it again we get the old values.
So the response json result is cached !!!

We are using  Awesome .net Web API Core + Entity Framework Core + Angular 2 :) "Amazing right"
So how we disable the caching / response json result !!!

- First i think about response header Cache related headers :
So i modified my Web API Core Service to disable Cache like the following:

- You can achieve this by adding this to your Controller action or over all Controller:
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]

- Guess what :) !!! Still not working hahaa

- After that i think from Angular perspective ,So i added timestamp to the current http request like this :

- Guess what :) !!! Also Still not working hmmm ok nice try.

- So i tried to think again from the perspective of EF Core , And add the ".AsNoTracking()" at the end of my query to disable EF cacheing .

- Guess what :) !!! WOrrrrrking Yaaaaaaaaaa :)

Thanks and i hope this will help someone over the univers

No comments: