0

I have the following Action method:-

[HttpGet]
[OutputCache(CacheProfile = "long", Location = OutputCacheLocation.Server, VaryByHeader = "X-Requested-With")]
public PartialViewResult LatestAssets()
{
var tech = repository.LatestTechnology().OrderByDescending(a => a.TechnologyID).Take(5).ToList() ;
return PartialView("_Latest",tech);
}

which is calling inside the _layout view through the following code:-

<li class="nav-header hidden-tablet"style="background-color:#3E9BD4 ; color:white">Latest Assets</li>
@Html.Action("LatestAssets","Home")

The Cache Profile is define inside the web.config as follow:-

 <system.web>
    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <add name="long" duration="300" />
          <add name="Short" duration="100" />
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>

but i am getting the following code when the action method is called:-

Duration must be a positive number. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Duration must be a positive number

And if i remove the CacheProfile the above will work fine. any idea what is causing this error.

haim770
  • 48,394
  • 7
  • 105
  • 133
John John
  • 1
  • 72
  • 238
  • 501
  • possible duplicate of [MVC HTML.RenderAction – Error: Duration must be a positive number](http://stackoverflow.com/questions/4997989/mvc-html-renderaction-error-duration-must-be-a-positive-number) – Ramesh Rajendran Aug 20 '13 at 10:37
  • `OutputCache` for ChildActions will not honor the Duration settings of your profile. You can get around it creating a custom OutputCache attribute to read in your profile settings. Here's how: http://stackoverflow.com/a/13866280/1373170 – Pablo Romeo Aug 25 '13 at 00:55

2 Answers2

0

You are missing the "Duration" property of the "OutputCache" attribute. Note that Duration sets the life of the cache in seconds, and it must be an integer greater than 0.

You are also missing the required "VaryByParam" property.

Try this:

[OutputCache(CacheProfile = "long", Location = OutputCacheLocation.Server, VaryByHeader = "X-Requested-With", Duration = 60, VaryByParam = "*")]

References:

MVC HTML.RenderAction – Error: Duration must be a positive number

http://www.dotnetcurry.com/ShowArticle.aspx?ID=665

Community
  • 1
  • 1
htxryan
  • 2,871
  • 2
  • 21
  • 21
-1

Just use:

[OutputCache(Duration = 1)]

One second is almost equal to zero and your problem will be fixed. It worked for me.

  • 1
    Put some thought into this ... at least consider all the implications. I tried this (not because you suggested it) and didn't realize page requests from different users were getting other users pages. A major authorization problem! This solution worked for me: http://stackoverflow.com/questions/10011780/prevent-caching-in-asp-net-mvc-for-specific-actions-using-an-attribute – micahhoover Mar 19 '15 at 16:16