本文介绍了保证一个ActionFilterAttribute的所有阶段被称为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在写,有人问我,如果有关于在ActionFilterAttribute.而我无法满怀信心地回答。

In writing this answer, I was asked if there are guarantees about the behaviour of the ActionFilterAttribute. and I was unable to answer with confidence.

在特定的,是所有四个方法 OnActionExecuted OnActionExecuting OnResultExecuted &安培; OnResultExecuting 保证被调用为通过属性,还是有情节的所有请求(如异常,断开的连接等),其中的一个或多个阶段可能不火?

In particular, are all four of the methods OnActionExecuted, OnActionExecuting, OnResultExecuted & OnResultExecuting guaranteed to be called for all requests that pass through the attribute, or are there circumstances (such as exceptions, dropped connection etc) where one or more of the phases might not fire?

推荐答案

没有他们不能保证被调用。

No they are not guaranteed to be called.

想想授权过滤器。如果授权失败,你会期望任何行动过滤器来运行?这可能是一个很大的安全漏洞。我相信,一个异常也将停止过滤器和唯一的例外过滤器管道将来自该点被执行。

Think of the Authorization filters. If authorization fails, would you expect any action filters to run? That could be a big security hole. I believe an exception would also stop the pipeline of filters and only exception filters would be executed from that point.

由于以下过滤器:

public class ExampleFilterAttribute : FilterAttribute, IActionFilter
{
    public void OnActionExecuted(ActionExecutedContext filterContext)
    {
        // this code is never reached...
    }

    public void OnActionExecuting(ActionExecutingContext filterContext)
    {
        throw new NotImplementedException();
    }
}

以下控制器动作:

on the following controller action:

[ExampleFilter]
public ActionResult Index()
{
    // this code is never reached...
    return View();
}

无论是指数()法或 OnActionExecuted()是不断达到,因为 OnActionExecuting()都有例外。

Neither the Index() method or the OnActionExecuted() is ever reached because OnActionExecuting() has an exception.

这篇关于保证一个ActionFilterAttribute的所有阶段被称为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 12:31