0

我有这个 OutputCache 的例子。我的问题是我希望页面仅在[id]等于时才被缓存NULL。在所有其他情况下,我根本不想拥有缓存。

我的控制器:

[OutputCache(Duration = int.MaxValue, VaryByParam = "id")]
public ActionResult Details(int id)
{}

路由配置:

routes.MapRoute(
    name: "edit",
    url: "edit/{id}",
    defaults: new {
        controller = "asd",
        action = "Details",
        id = UrlParameter.Optional
    }
);
4

1 回答 1

1

您可以指定(并实现)以下VaryByCustom参数OutputCacheAttribute

我的控制器.cs

[OutputCache(Duration = int.MaxValue, VaryByCustom = "idIsNull")]
public ActionResult Details(int id)
{
}

全球.asax.cs

public override string GetVaryByCustomString(HttpContext context, string arg)
{
    if (arg.ToLower() == "idisnull")
    {
        return string.IsNullOrWhiteSpace(Request.QueryString["id"])
            ? string.Empty
            // unique key means it won't have a consistent value to use
            // as a cache lookup
            : ((new DateTime(1970, 1, 1) - DateTime.Now).TotalMilliseconds).ToString();
    }
}
于 2013-09-27T15:36:24.430 回答