MVC3剃须刀 - 网页过期 [英] MVC3 Razor - Expiring pages

查看:139
本文介绍了MVC3剃须刀 - 网页过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这样当用户点击浏览器的导航(后退)按钮控制器动作被执行到期我的内容。因此,而不是添加以下code到每一个结果
行动有没有更好的方式来做到这一点。

  HttpContext.Response.Expires = -1;
HttpContext.Response.Cache.SetNoServerCaching();
Response.Cache.SetAllowResponseInBrowserHistory(假);
Response.CacheControl =无缓存;
Response.Cache.SetNoStore();


解决方案

您可以把这个逻辑到ActionFilter这意味着,而不是将上述code到每个在你的控制器动作方法,你可以装饰操作方法与您的自定义过滤器。或者,如果它适用于在控制器的所有动作方法可以将属性应用到整个控制器。

您ActionFilter将是这样的:

 公共类MyExpirePageActionFilterAttribute:System.Web.Mvc.ActionFilterAttribute
    {
        公共覆盖无效OnActionExecuted(System.Web.Mvc.ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);            filterContext.HttpContext.Response.Expires = -1;
            filterContext.HttpContext.Response.Cache.SetNoServerCaching();
            filterContext.HttpContext.Response.Cache.SetAllowResponseInBrowserHistory(假);
            filterContext.HttpContext.Response.CacheControl =无缓存;
            filterContext.HttpContext.Response.Cache.SetNoStore();        }
    }

请参阅文章以了解更多信息。

如果你想在这整​​个应用程序的所有操作,你可以实际应用的ActionFilter使用一个全球性的ActionFilter在Global.asax中设置的所有操作:

 保护无效的Application_Start()
{
    AreaRegistration.RegisterAllAreas();    GlobalFilters.Filters.Add(新MyExpirePageActionFilterAttribute());    RegisterGlobalFilters(GlobalFilters.Filters);
    的RegisterRoutes(RouteTable.Routes);
}

I need to expire my content so that when the user hits the browsers navigation(back) button the controller action gets executed. So instead of adding the following code to each and every
action is there a better way to do it.

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

解决方案

You can put this logic into an ActionFilter meaning that rather than adding the above code to each of your Action methods in your controller, you can just decorate the Action method with your custom filter. Or if it applies to all Action methods in a Controller you can apply the attribute to the whole Controller.

Your ActionFilter will be something like this:

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();

        }
    }

See this article for more information.

If you want this on all Actions of your entire application, you can actually apply an ActionFilter to all Actions using a global ActionFilter set up in your Global.asax:

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

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

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

这篇关于MVC3剃须刀 - 网页过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆