为什么没有我的ActionFilters运行? [英] Why do none of my ActionFilters run?

查看:304
本文介绍了为什么没有我的ActionFilters运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<我一个href=\"http://stackoverflow.com/questions/933798/how-do-i-access-the-modelstate-form-an-actionfilter\">asked在ASP.Net MVC的问题今天早些时候关于 ActionFilters 。原来,我的问题是真的,我的 ActionFilter 甚至没有运行。别的不说我读<一个href=\"http://ittecture.word$p$pss.com/2009/04/25/tip-of-the-day-193-aspnet-mvc-custom-action-filters/\"相对=nofollow>这篇文章,我找不到任何他不说我不知道​​。

I asked a question earlier today about ActionFilters in ASP.Net MVC. It turned out my problem was really that my ActionFilter is not even running. Among other things I read this article, and I can't find anything he does that I don't.

这是我的code:

// The ActionFilter itself
public class TestingIfItWorksAttribute : ActionFilterAttribute
{
	public override void OnActionExecuting(ActionExecutingContext filterContext)
	{
		filterContext.Controller.TempData["filter"] = "it worked!";
		base.OnActionExecuting(filterContext);
	}
}

// The Controller Action with the filter applied
[TestingIfItWorks]
public ActionResult Test()
{
	var didit = TempData["filter"];
	return View();
}

当我调试过滤方法的断点永远不会命中,而的TempData [过滤器] 持有视图时呈现的价值。

A breakpoint in the filter method is never hit when I debug, and TempData["filter"] holds a null value when the view is rendered.

这是为什么不工作?

推荐答案

根据您的意见,以另一种答案

在通过单元测试检验,过滤器不被调用。如果你想调用的过滤器,那么你就需要模仿ControllerActionInvoker。它可能会更好,以测试过滤器本身孤立,然后使用反射来确保过滤器被应用到你的动作与正确的属性。我在联合测试过滤器和动作preFER这个机制。

When testing via unit tests, the filter is not invoked. If you want to invoke the filter then you'll need mimic the ControllerActionInvoker. It's probably better, to test the filter itself in isolation, then use reflection to ensure that the filter is applied to your action with the correct attributes. I prefer this mechanism over testing the filter and action in combination.

原始

当然,你需要在你的方法,否则你实际上并没有取代基类的方法的覆盖。我本来期望编译器抱怨你需要的是覆盖就可以了。如果不包括覆盖关键字,它的行为就好像您使用。由于框架调用它作为一个ActionFilterAttribute,这意味着你的方法永远不会被调用。

Surely you need an override on your method otherwise you aren't actually replacing the method on the base class. I would have expected the compiler to complain that you needed either a new or override on it. If you don't include the override keyword, it will behave as if you used new. Since the framework invokes it as an ActionFilterAttribute, this means that your method will never get called.

MSDN 报价:

如果在派生类中的方法是
  没有$ P $通过新的或重写pceded
  关键字,编译器会发出
  警告和方法将表现为
  如果新的关键字为present。

If the method in the derived class is not preceded by new or override keywords, the compiler will issue a warning and the method will behave as if the new keyword were present.

这篇关于为什么没有我的ActionFilters运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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