繁体   English   中英

如何从操作返回缓存的操作结果

[英]How to return cached actionresult from an action

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    if (filterContext.Controller.ViewData["currentuser"] == null)
    {
        filterContext.Result = new HomeController().IndexCache();
    }
    base.OnActionExecuting(filterContext);
}

[OutputCache(Duration=3600)]
public ActionResult IndexCache()
{
    return view()
}

这是行不通的。 如果用户未登录,我想返回缓存。 这不是最终代码。

在为它们缓存Index()之前,我需要检查action =“ index”和controller =“ home”。

当我创建控制器的新实例不被缓存。 有人可以告诉我如何在此代码中返回缓存的索引吗?

您需要重定向到类似

filterContext.Result =  new RedirectToRouteResult(
                    new System.Web.Routing.RouteValueDictionary {
                    { "controller", "Home" },
                    { "action", "IndexCache" });

而不是使函数调用new HomeController().IndexCache();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM