- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个全局 NoCache 过滤器,如下所示:https://stackoverflow.com/a/12964123/78739
这个全局无缓存过滤器适用于所有操作。我有一个案例,我需要允许使用 OutputCacheAttribute 缓存一个特定的操作。我在考虑在 NoCache 过滤器中检查刚刚执行的操作是否具有 OutputCacheAttribute。如果是这样,它不会应用无缓存设置。因此,例如,我的代码将是:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public sealed class NoCacheAttribute : FilterAttribute, IResultFilter
{
public void OnResultExecuted(ResultExecutedContext filterContext)
{
if (/* action does not have OutputCacheAttribute */)
{
var cache = filterContext.HttpContext.Response.Cache;
cache.SetCacheability(HttpCacheability.NoCache);
cache.SetRevalidation(HttpCacheRevalidation.ProxyCaches);
cache.SetExpires(DateTime.Now.AddYears(-5));
cache.AppendCacheExtension("private");
cache.AppendCacheExtension("no-cache=Set-Cookie");
cache.SetProxyMaxAge(TimeSpan.Zero);
}
}
}
问题是我不知道如何从传递到 OnResultExecuted 方法的 ResultExecutedContext 变量中获取操作及其属性。
最佳答案
这个答案是针对 MVC4 的,因为 MVC5 有 filter overrides
从 OnResultExecuted
ActionDescriptor
似乎没有一个干净的方法(我避免反射。讨厌!)
与您提供的 SO 链接中未接受的答案相反,OutputCacheAttribute 确实控制客户端缓存,因此:
在全局范围内
filters.Add(new OutputCacheAttribute { Location = OutputCacheLocation.None, NoStore = true });
然后在行动
//The OutputCacheAttribute in the global filter won't be called
//Only this OutputCacheAttribute is called since AllowMultiple in OutputCacheAttribute is false
[[OutputCacheAttribute(Location = OutputCacheLocation.Server, Duration = 100)]
public ActionResult Index()
{
return View();
}
检查响应 header 以验证
关于c# - 需要从 OnResultExecuted 获取 Action 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21566756/
我正在创建一个自定义 ActionFilterAttribute为了转型ViewResult s 并重定向到 JsonResult s 在 ajax 请求期间。我已经通过单元测试连接了大部分内容,出于
我正在尝试创建一个自定义 ActionFilter,它将在执行操作结果之前运行。此属性将获取存储在 TempData 中的错误信息,并将其插入到主强类型模型中的子模型 (ErrorModel) 中。但
OnActionExecuted 和 OnResultExecuting 有什么区别?一旦处理完 Action ,它们会立即被解雇,还是在这两种方法之间发生某些事情。就像 View 引擎的初始化一样,
我有一个“无 session ” Controller ,其操作返回 JsonResult。我的客户端 Javascript 使用 Jquery ajax 通过 HTTP GET 调用此操作 [Htt
我有一个应用了属性的 Controller 操作,如果 ModelState 有错误,它会将它们设置为 OnResultExecuting 方法中的 JsonResult。 我在 MyAction 中
我有一个全局 NoCache 过滤器,如下所示:https://stackoverflow.com/a/12964123/78739 这个全局无缓存过滤器适用于所有操作。我有一个案例,我需要允许使用
简单的问题。如何覆盖 Controller 中的 OnResultExecuting 方法并强制使用不同的 ActionResult? 示例代码如下。目前,原始的 ActionResutl 继续执行,
我需要仅过滤 Controller 操作中具有返回类型 ActionResult 的所有操作中的方法。我从以下获取 Controller 名称和操作名称.. string originControll
这个问题在这里已经有了答案: How to disable a global filter in ASP.Net MVC selectively (7 个答案) 关闭 4 年前。 我正在开发一个 M
我正在寻找 OnActionExecuting 之间的比较和 OnResultExecuting . 什么时候应该与 OnActionExecuting 一起工作,以及与 OnResultExecut
ASP.NET MVC:在ActionFilterAttribute类中,OnActionExecuted和OnResultExecuted有什么区别?实现压缩过滤器时,压缩代码应该放在哪些方法中?
我是一名优秀的程序员,十分优秀!