如何跳过从ActionFilter动作执行? [英] How to skip action execution from an ActionFilter?

查看:866
本文介绍了如何跳过从ActionFilter动作执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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");
}

这篇关于如何跳过从ActionFilter动作执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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