Caching HTML with Cache TagHelpers
In this recipe, we will learn how to use Cache TagHelpers.
Getting ready
TagHelpers let developers use their C# classes and methods in CSHTML files. With TagHelpers, developers can reuse most of the C# codes and logics in CSHTML files.
We will now create a view where we will use Cache TagHelper to cache elements in a view.
How to do it...
- First, we use the TagHelper
expires-after:
<!-- expires-after -->
<cache expires-after="@TimeSpan.FromMinutes(10)">
@Html.Partial("_MyPartialView")
</cache>- Next, we use the TagHelper
expires-on:
<!-- expires-on -->
<cache expires-on="@DateTime.Today.AddDays(1).AddTicks(-1)">
@Html.Partial("_MyPartialView")
</cache>- Next, we use the TagHelper
expires-sliding:
<!-- expires-sliding -->
<cache expires-sliding="@TimeSpan.FromMinutes(5)">
@Html.Partial("_MyPartialView")
</cache>
- Next, we use the TagHelper
vary-by-user...