2013-08-20 57 views
0

我有以下操作方法: -爲什麼我收到「時長必須爲正數。」,定義CacheProfile時

[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); 
} 

這是通過下面的代碼_layout視圖中呼籲: -

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

緩存配置文件被定義在web.config如下內: -

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

,但我得到的FOL降脂代碼時的操作方法被稱爲: -

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

如果我刪除CacheProfile以上將正常工作。 任何想法是什麼導致此錯誤。

+0

可能重複的[MVC HTML.RenderAction - 錯誤:持續時間必須是正數](http://stackoverflow.com/questions/4997989/mvc-html-renderaction-error-duration-must-bea-a-正數) –

+0

ChildActions的'OutputCache'不符合配置文件的持續時間設置。您可以繞過它創建一個自定義的OutputCache屬性來讀取您的配置文件設置。這是如何:http://stackoverflow.com/a/13866280/1373170 –

回答

0

您錯過了「OutputCache」屬性的「Duration」屬性。請注意,持續時間以秒爲單位設置緩存的生命週期,並且它必須是大於0的整數。

您還缺少所需的「VaryByParam」屬性。

嘗試這種情況:

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

參考文獻:

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

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

0

只需使用:

[的OutputCache(持續時間= 1)]

一秒鐘幾乎等於零,您的問題將得到解決。它爲我工作。

+0

把一些想法,至少考慮所有的影響。我試過這個(不是因爲你提出這個建議),也沒有意識到來自不同用戶的頁面請求正在獲取其他用戶頁面。一個主要的授權問題!此解決方案爲我工作:http://stackoverflow.com/questions/10011780/prevent-caching-in-asp-net-mvc-for-specific-actions-using-an-attribute – micahhoover

相關問題