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); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-web-api – 为WCF REST和ASP.NET Web API共享相同的
- asp.net-mvc – 如何处理MVC5中的配置和代码中的404错误?
- asp.net-mvc – 对Json解决方法的EntityFramework? (序列化
- iis-7 – localhost上的IIS非常慢
- 为什么我不能将ASP.NET Framework版本显示为3.5?
- ASP.Net是适用于高负载网站的技术吗?
- reactjs – TypeError:无法读取undefined的属性’ts’
- asp.net – 我可以强制刷新我的样式表文件吗?
- asp.net-mvc – 如何创建ASP.NET MVC控制器从查询字符串接受
- 什么是Asp.net中的中等信任?
推荐文章
站长推荐
- 如何限制文件夹访问在asp.net
- asp.net – 如何配置IIS以接受POST请求?
- asp.net-mvc – ASP.NET MVC 5如何在Identity 2.
- asp.net-mvc – 为什么编码逗号URL?
- asp.net – TransferRequestHandler和BeginReque
- asp.net – 有没有办法禁用.NET浏览器检测?
- asp.net-mvc – Nuget如何指定包位置?
- asp.net-mvc – 在活动的SignalR连接期间更改用户
- asp.net-mvc – 绑定到MVC中的SelectList
- asp.net-mvc – 错误NU1002项目WebApplication1中
热点阅读