Asp.Net Core - using responseCache with ViewComponent

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 this is how I use it in my view:

@await Component.InvokeAsync("Categories")

      

I tried to use caching by putting:

[ResponseCache(Duration = 99999999)]

      

above call method but doesn't work.

Can caching be used with view components? And How?

+3


source to share


1 answer


You can cache View Components by wrapping them in a cache tag helper like:



<cache expires-after="@TimeSpan.FromMinutes(5)">
    @await Component.InvokeAsync("Categories")
</cache>

      

+4


source







All Articles