here's my ViewComponent:
public class Categories: ViewComponent
{
private readonly ICategoryService _categories;
public Categories(ICategoryService categories)
{
_categories = categories;
}
public IViewComponentResult Invoke()
{
var cat = _categories.GetCategories();
return View(viewName: "Default", model: cat);
}
}
and here's how I use it in my view:
@await Component.InvokeAsync("Categories")
I tried use caching by putting:
[ResponseCache(Duration = 99999999)]
above invoke method but didn't work.
Is it possible to use caching with view components? and how?
You can cache View Components by wrapping them in a cache tag helper, like this:
<cache expires-after="@TimeSpan.FromMinutes(5)">
@await Component.InvokeAsync("Categories")
</cache>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With