can you use output caching in asp.net-mvc based on the parameters of your controller action

北城以北 提交于 2019-12-10 18:53:54

问题


i want to use output caching to avoid hitting my db over and over with the same static query but my controllers have parameters that uniquely define the post.

How can i factor in my parameters and still support output caching in asp.net-mvc?


回答1:


Check out the VaryByParam property of the OutputCache attribute.

[OutputCache(Duration=int.MaxValue, VaryByParam="id")]
public ActionResult Details(int id)
{
}

For each unique id value, a unique cache instance will be created.

Edit:

If your caching needs go beyond simple VaryByParam scenarios then take a look at VaryByCustom. This will allow you to setup the scenarios as you see fit (cached version for logged in vs. not logged in user, etc. etc.)



来源:https://stackoverflow.com/questions/6339191/can-you-use-output-caching-in-asp-net-mvc-based-on-the-parameters-of-your-contro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!