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

asp.net – MVC3 Razor – 到期页面

发布时间:2020-12-15 19:26:37 所属栏目:asp.Net 来源:网络整理
导读:我需要使我的内容过期,以便当用户点击浏览器导航(返回)按钮时,控制器动作被执行.所以不是每个都添加以下代码 行动是有更好的方式来做到这一点. HttpContext.Response.Expires = -1;HttpContext.Response.Cache.SetNoServerCaching();Response.Cache.SetAllow
我需要使我的内容过期,以便当用户点击浏览器导航(返回)按钮时,控制器动作被执行.所以不是每个都添加以下代码
行动是有更好的方式来做到这一点.
HttpContext.Response.Expires = -1;
HttpContext.Response.Cache.SetNoServerCaching();
Response.Cache.SetAllowResponseInBrowserHistory(false);
Response.CacheControl = "no-cache";
Response.Cache.SetNoStore();

解决方法

您可以将此逻辑放入ActionFilter中,而不是将上述代码添加到控制器中的每个Action方法中,您可以使用自定义过滤器来装饰Action方法.或者如果它适用于Controller中的所有Action方法,您可以将属性应用于整个Controller.

你的ActionFilter将是这样的:

public class MyExpirePageActionFilterAttribute : System.Web.Mvc.ActionFilterAttribute
    {
        public override void OnActionExecuted(System.Web.Mvc.ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);

            filterContext.HttpContext.Response.Expires = -1;
            filterContext.HttpContext.Response.Cache.SetNoServerCaching();
            filterContext.HttpContext.Response.Cache.SetAllowResponseInBrowserHistory(false);
            filterContext.HttpContext.Response.CacheControl = "no-cache";
            filterContext.HttpContext.Response.Cache.SetNoStore();

        }
    }

有关更多信息,请参阅this文章.

如果您希望在整个应用程序的所有操作上执行此操作,则实际上可以使用Global.asax中设置的全局ActionFilter将ActionFilter应用于所有操作:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    GlobalFilters.Filters.Add(new MyExpirePageActionFilterAttribute());

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
}

(编辑:李大同)

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

    推荐文章
      热点阅读