asp.net – MVC3 Razor – 到期页面
发布时间:2020-05-23 04:56:08 所属栏目:asp.Net 来源:互联网
导读:我需要使我的内容过期,以便当用户点击浏览器导航(返回)按钮时,控制器动作被执行.所以不是每个都添加以下代码 行动是有更好的方式来做到这一点. HttpContext.Response.Expires = -1;HttpContext.Response.Cache.SetNoServerCaching();Response.Cache.SetAllowR
|
我需要使我的内容过期,以便当用户点击浏览器导航(返回)按钮时,控制器动作被执行.所以不是每个都添加以下代码
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);
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 如何在ASP.net MVC中正确测试具有数据库调用
- asp.net – ApplicationInstance.CompleteRequest不会停止执
- asp.net-mvc – HttpContext.Items与ASP.NET MVC
- ASP.NET Page_Init被解雇了两次!
- asp.net-mvc – 有什么图形设计器ASP.NET MVC吗?
- asp.net – 如何使用Membership API与自己的应用程序相关数
- asp.net-web-api – 如何从ASP.net 5 web api返回文件
- asp.net-mvc – 失败的ASP.NET MVC路由.这是一个错误还是角
- asp.net – 如何防止Entity Framework将FileStream列加载到
- asp.net-mvc – ASP.NET MVC如何在生产中禁用调试路由/视图
推荐文章
站长推荐
- asp.net-mvc-3 – MVC3:如何在HtmlHelper扩展中
- asp.net-mvc – ASP.Net MVC 3 – JSON模型绑定到
- asp.net-mvc-4 – 什么是antlr3,为什么默认情况下
- asp.net-mvc – 如何在ASP.NET MVC w / VB.NET中
- ASP.NET MVC 4全局授权过滤器强制在AllowAnonymo
- asp.net-mvc-2 – EditorFor – 传入字典的模型项
- asp.net – 异步HttpWebRequest,从Web应用程序中
- asp.net-mvc – MVC:如何将文件上传和其他表单字
- asp.net – IE 11中的报表查看器打印按钮
- asp.net-mvc – 每个实现的存储库模式的优缺点
热点阅读
