gpt4 book ai didi

asp.net-mvc - 属性的 OnActionExecuted 方法是否总是执行?

转载 作者:行者123 更新时间:2023-12-03 18:21:36 25 4
gpt4 key购买 nike

我搜索了高低,似乎找不到直接的答案。

如果我有自定义属性/过滤器,OnActionExecuted 是否会出现?方法总是被调用?即使有异常抛出?

最佳答案

至少在 MVC 5 中,@tvanfosson 的答案不再正确。这也可能适用于早期版本。
OnActionExecuted总是被调用并且可以通过 filterContext.Exception 访问抛出的异常.

一个 Action 中有异常的测试用例:

public class HomeController
: Controller
{
public ActionResult Index()
{
throw new Exception("Index");
}
}

public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new Foo());
filters.Add(new Bar());
}
}

public class Foo
: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Debug.WriteLine($"{nameof(Foo)}.{nameof(OnActionExecuting)}");
base.OnActionExecuting(filterContext);
}

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
Debug.WriteLine($"{nameof(Foo)}.{nameof(OnActionExecuted)}");
Debug.WriteLine($"Has exception: {filterContext.Exception != null}");
base.OnActionExecuted(filterContext);
}
}

public class Bar
: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Debug.WriteLine($"{nameof(Bar)}.{nameof(OnActionExecuting)}");
base.OnActionExecuting(filterContext);
}

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
Debug.WriteLine($"{nameof(Bar)}.{nameof(OnActionExecuted)}");
Debug.WriteLine($"Has exception: {filterContext.Exception != null}");
base.OnActionExecuted(filterContext);
}
}

输出:

Bar.OnActionExecuting
Foo.OnActionExecuting
Exception thrown: 'System.Exception' in WebApplication1.dll
Foo.OnActionExecuted
Has exception: True
Bar.OnActionExecuted
Has exception: True

过滤器中有异常的测试用例
public class HomeController
: Controller
{
public ActionResult Index()
{
return new HttpStatusCodeResult(200);
}
}

public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new Foo());
filters.Add(new Bar());
}
}

public class Foo
: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Debug.WriteLine($"{nameof(Foo)}.{nameof(OnActionExecuting)}");
base.OnActionExecuting(filterContext);
}

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
Debug.WriteLine($"{nameof(Foo)}.{nameof(OnActionExecuted)}");
Debug.WriteLine($"Has exception: {filterContext.Exception != null}");
throw new Exception("Foo");
base.OnActionExecuted(filterContext);
}
}

public class Bar
: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Debug.WriteLine($"{nameof(Bar)}.{nameof(OnActionExecuting)}");
base.OnActionExecuting(filterContext);
}

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
Debug.WriteLine($"{nameof(Bar)}.{nameof(OnActionExecuted)}");
Debug.WriteLine($"Has exception: {filterContext.Exception != null}");
base.OnActionExecuted(filterContext);
}
}

输出:

Bar.OnActionExecuting
Foo.OnActionExecuting
Foo.OnActionExecuted
Has exception: False
Exception thrown: 'System.Exception' in WebApplication1.dll
Bar.OnActionExecuted
Has exception: True

关于asp.net-mvc - 属性的 OnActionExecuted 方法是否总是执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1650108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com