asp.net – MVC3 Razor – 到期页面

我需要使我的内容过期,以便当用户点击浏览器导航(返回)按钮时,控制器动作被执行.所以不是每个都添加以下代码
行动是有更好的方式来做到这一点.
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);
}

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...