次のアクションがあります。
public class HomeController : Controller
{
public ActionResult Index(int? id) { /* ... */ }
}
私は[OutputCache]
そのアクションをしたいのですが、次のいずれかが欲しいです:
- 次の場合はキャッシュを使用しません
id == null
。また id == null
ただし、期間が異なる場合はキャッシュを使用します。
私はこれを達成できると思います:
public class HomeController : Controller
{
[OutputCache(VaryByParam = "none", Duration = 3600)]
public ActionResult Index() { /* ... */ }
[OutputCache(VaryByParam = "id", Duration = 60)]
public ActionResult Index(int id) { /* ... */ }
}
id
ただし、このソリューションは、実際にはオプションである場合に 2 つのアクションを意味するため、コードの繰り返しが発生する可能性があります。もちろん、私は次のようなことをすることができます
public class HomeController : Controller
{
[OutputCache(VaryByParam = "none", Duration = 3600)]
public ActionResult Index() { return IndexHelper(null); }
[OutputCache(VaryByParam = "id", Duration = 60)]
public ActionResult Index(int id) { return IndexHelper(id); }
private ActionResult IndexHelper(int? id) { /* ... */ }
}
しかし、これは醜いようです。
これをどのように実装しますか?