The error message that "Duration must be a positive number" is really annoying. I understand the Location field doesn't make sense for child actions, but why can't it just be ignored?
I'd like to be able to have this class
[OutputCache(Location = OutputCacheLocation.None)]
public class AccountController : Controller{
public ActionResult MyAccount(AccountModel model) {
return View(model);
}
[ChildActionOnly]
public ActionResult UtilNav(AccountModel model) {
return PartialView(model);
}
}
without sticking a separate OutputCache attribute on each partial method.
Not sure if this helps, but here is how someone got around that problem. You will need to modify the ASP.NET MVC source or make your own attribute ("MyOutputCache" for example) that implements this functionality:
public class MyOutputCacheAttribute : OutputCacheAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
try
{
base.OnActionExecuting(filterContext);
}
catch (InvalidOperationException ex)
{
if (!filterContext.IsChildAction || ex.Message != "Duration must be a positive number.")
throw;
//else swallow. This may be dangerious though
}
}
}
[MyOutputCacheAttribute(Location = OutputCacheLocation.None)]
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
adraper
Member
5 Points
5 Posts
An MVC request: allow OutputCache attribute on partial methods
Feb 16, 2012 07:15 PM|LINK
The error message that "Duration must be a positive number" is really annoying. I understand the Location field doesn't make sense for child actions, but why can't it just be ignored?
I'd like to be able to have this class
[OutputCache(Location = OutputCacheLocation.None)] public class AccountController : Controller{ public ActionResult MyAccount(AccountModel model) { return View(model); } [ChildActionOnly] public ActionResult UtilNav(AccountModel model) { return PartialView(model); } }without sticking a separate OutputCache attribute on each partial method.
DarrellNorto...
All-Star
86665 Points
9634 Posts
Moderator
MVP
Re: An MVC request: allow OutputCache attribute on partial methods
Feb 18, 2012 10:30 AM|LINK
Not sure if this helps, but here is how someone got around that problem. You will need to modify the ASP.NET MVC source or make your own attribute ("MyOutputCache" for example) that implements this functionality:
http://thenullreference.com/blog/fixing-the-asp-net-mvc-3-outputcacheattribute-for-partial-views-to-honor-some-web-config-settings/
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: An MVC request: allow OutputCache attribute on partial methods
Feb 20, 2012 05:06 AM|LINK
A really dirtly solution may be,
public class MyOutputCacheAttribute : OutputCacheAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { try { base.OnActionExecuting(filterContext); } catch (InvalidOperationException ex) { if (!filterContext.IsChildAction || ex.Message != "Duration must be a positive number.") throw; //else swallow. This may be dangerious though } } } [MyOutputCacheAttribute(Location = OutputCacheLocation.None)]Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
adraper
Member
5 Points
5 Posts
Re: An MVC request: allow OutputCache attribute on partial methods
Jan 15, 2013 01:28 AM|LINK
For what it's worth, here's what I used, and it seems to be working.
public class ControllerOutputCacheAttribute : System.Web.Mvc.OutputCacheAttribute{ bool IgnoreChildCache(System.Web.Mvc.ControllerContext filterContext) { return filterContext.IsChildAction && (Location == System.Web.UI.OutputCacheLocation.None || Duration == 0); } public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext) { if(IgnoreChildCache(filterContext)) { return; } else{ base.OnActionExecuting(filterContext); } } public override void OnResultExecuted(System.Web.Mvc.ResultExecutedContext filterContext) { if(IgnoreChildCache(filterContext)) { return; } else { base.OnResultExecuted(filterContext); } } public override void OnResultExecuting(System.Web.Mvc.ResultExecutingContext filterContext) { if(IgnoreChildCache(filterContext)) { return; } else { base.OnResultExecuting(filterContext); } } }