加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 在操作过滤器上使用缓存数据,以避免再次执行操作

发布时间:2020-12-16 07:09:45 所属栏目:asp.Net 来源:网络整理
导读:我想做以下(我将分为两点): 在执行操作之前,如果viewmodel位于缓存中,则返回视图和viewmodel而不执行操作. 如果不在缓存中,则继续执行操作,并到达OnActionExecuted以将viewmodel放入缓存中. 如何在不执行操作的情况下返回视图和视图模型(第一点)? 这是代码
我想做以下(我将分为两点):

>在执行操作之前,如果viewmodel位于缓存中,则返回视图和viewmodel而不执行操作.
>如果不在缓存中,则继续执行操作,并到达OnActionExecuted以将viewmodel放入缓存中.

如何在不执行操作的情况下返回视图和视图模型(第一点)?

这是代码.我怀疑用???????表示:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读