far*_*g67 7 asp.net asp.net-core-mvc asp.net-core
这是我的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);
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我在视图中使用它的方法:
@await Component.InvokeAsync("Categories")
Run Code Online (Sandbox Code Playgroud)
我尝试使用缓存:
[ResponseCache(Duration = 99999999)]
Run Code Online (Sandbox Code Playgroud)
上面调用方法但没有工作.
是否可以使用视图组件缓存?如何?
Wil*_*Ray 14
您可以通过将View Components包装在缓存标记帮助程序中来缓存它们,如下所示:
<cache expires-after="@TimeSpan.FromMinutes(5)">
@await Component.InvokeAsync("Categories")
</cache>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
879 次 |
| 最近记录: |