ASP.NET Core 2.0 MVC Distributed Cache Tag Helper


How to use Distributed Cache Tag Helper in ASP.NET Core MVC.


In an empty project update Startup class to add services and middleware for MVC and Distributed Cache:

Add a controller:

Add a _ViewImports.cshtml page in Views folder. Add directive to import tag helpers:

Add a razor page (Index.cshtml):

Run the sample, keep refreshing and you’ll notice that Cached Time doesn’t refresh (until cache expires):


Cache and Distributed Cache Tag Helper help improve performance of your application by caching view’s content, either in-memory or in a distributed cache (e.g. Redis).

Distributed Cache Tag Helper uses IDistributedCache to store contents in a distributed cache. To learn more about distributed caching, please refer to an earlier post here.


Cache Tag Helper has following attributes:


Expiry related attributes control length of time cache is valid for. You could specify an absolute or sliding date/time.


You could invalidate the cache if HTTP header, query string, routing data, cookie value or custom values.

Source Code


Leave a Reply