簡體   English   中英

在動作過濾器上使用緩存數據,以避免再次執行動作

[英]Use cached data on an action filter, to avoid another execution of an action

我想做以下事情(我將分為兩點):

  • 在執行操作之前,如果視圖模型在緩存中,則返回視圖和視圖模型而不執行操作。

  • 如果不在緩存中,則繼續執行操作,並到達 OnActionExecuted 以將視圖模型放入緩存中。

我如何在不執行操作(第一點)的情況下返回視圖和視圖模型?

這是代碼。 我的疑問表示為?????????:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
   //IF the viewmodel exists dont execute the action again
   if (filterContext.HttpContext.Cache["viewmodel"]!=null)
   {
      filterContext.Result=???????
   }
   base.OnActionExecuting(filterContext);
}

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
    //Cast de model
    ContentDetailVM model = (ContentDetailVM)filterContext.Controller.ViewData.Model;
    filterContext.HttpContext.Cache.Insert("viewmodel", model);
    //we're asking for a close section
    if (model.CurrentSection.HideAccess == true)
    {
         //pass to the client some flag in order to show the div
         filterContext.Controller.ViewData["showoverlaylayer"]=true;
    }
    base.OnActionExecuted(filterContext);     
}

提前非常感謝。

此致。

何塞。

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    var model = filterContext.HttpContext.Cache["viewmodel"];
    if (model != null)
    {
        var result = new ViewResult();
        result.ViewData.Model = model;
        filterContext.Result = result;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM