如何跳过从ActionFilter动作执行?

问题描述:

时可以跳过这个操作方法执行,并返回一个特定的ActionResult 特定条件时,在 OnActionExecuting $ C $会见C>?

Is it possible to skip the whole action method execution and return a specific ActionResult when a certain condition is met in OnActionExecuting?

请参阅我的下载样品和MSDN文章的筛选在ASP.NET MVC

See my download sample and MSDN article Filtering in ASP.NET MVC.

您可以取消在 OnActionExecuting OnResultExecuting 通过设置结果的方法过滤器执行属性设置为一个非空值。

You can cancel filter execution in the OnActionExecuting and OnResultExecuting methods by setting the Result property to a non-null value.

任何挂起 OnActionExecuted OnActionExecuting 过滤器将不会被调用,调用者就不会调用 OnActionExecuted 为取消过滤器或待处理的过滤器的方法。

Any pending OnActionExecuted and OnActionExecuting filters will not be invoked and the invoker will not call the OnActionExecuted method for the cancelled filter or for pending filters.

为$ P $的 OnActionExecuted 过滤pviously运行过滤器将运行。所有 OnResultExecutingand OnResultExecuted 过滤器将运行。

The OnActionExecuted filter for previously run filters will run. All of the OnResultExecutingand OnResultExecuted filters will run.

从样品以下code显示了如何返回一个特定的的ActionResult 特定条件时,在 OnActionExecuting $ C满足$ C>:

The following code from the sample shows how to return a specific ActionResult when a certain condition is met in OnActionExecuting:

if (filterContext.RouteData.Values.ContainsValue("Cancel")) 
{
    filterContext.Result = new RedirectResult("~/Home/Index");
    Trace.WriteLine(" Redirecting from Simple filter to /Home/Index");
}