【问题标题】:why i am getting "Duration must be a positive number.", when defining a CacheProfile为什么我在定义 CacheProfile 时得到“持续时间必须是正数。”
【发布时间】:2014-10-04 00:47:09
【问题描述】:

我有以下Action方法:-

[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>

但是当调用操作方法时我得到以下代码:-

持续时间必须是正数。描述:一个未处理的 执行当前 Web 请求期间发生异常。 请查看堆栈跟踪以获取有关错误的更多信息和 它起源于代码的地方。

异常详细信息:System.InvalidOperationException:持续时间必须是 一个正数

如果我删除了 CacheProfile,上述内容将正常工作。 知道是什么导致了这个错误。

【问题讨论】:

标签: asp.net asp.net-mvc asp.net-mvc-3


【解决方案1】:

您缺少“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

【讨论】:

    【解决方案2】:

    只需使用:

    [输出缓存(持续时间 = 1)]

    一秒几乎等于零,您的问题将得到解决。它对我有用。

    【讨论】:

    • 对此进行一些思考......至少考虑所有含义。我尝试了这个(不是因为你建议它)并且没有意识到来自不同用户的页面请求正在获取其他用户页面。一个重大的授权问题!这个解决方案对我有用:stackoverflow.com/questions/10011780/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    相关资源
    最近更新 更多