C# 访问IEExceptionFilter中的ActionArguments
是否可以在C# 访问IEExceptionFilter中的ActionArguments,c#,asp.net-core,action-filter,C#,Asp.net Core,Action Filter,是否可以在IEExceptionFilter中访问ActionArguments 根据上面的图片,异常过滤器在有权访问它们的操作过滤器之后运行。所以我不明白他们为什么不能 在ActionFilter中,您可以这样访问它 public class MyFilter : IActionFilter { public void OnActionExecuting(ActionExecutingContext context) { context.ActionArgum
IEExceptionFilter
中访问ActionArguments
根据上面的图片,异常过滤器在有权访问它们的操作过滤器之后运行。所以我不明白他们为什么不能
在ActionFilter中,您可以这样访问它
public class MyFilter : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context)
{
context.ActionArguments // I need access to this in order to find out what type the input argument(s) was.
}
}
但是IEExceptionFilter使用的是公共void OneException(ExceptionContext上下文)
。我发现了:)
公共类例外过滤器:IEExceptionFilter,IActionFilter
{
私人词典;
公共无效OneException(ExceptionContext上下文)
{
_actionArguments//非常成功!
}
ActionExecuting(ActionExecutingContext上下文)上的公共void
{
_actionArguments=context.actionArguments;
}
public void OnActionExecuted(ActionExecutedContext上下文)
{
//无所事事
}
}
您具体想访问什么?我需要映射FluentValidation的ValidationExceptions中的PropertyNames,以匹配API模型PropertyNames。所以我需要知道使用了什么输入模型。我基本上需要访问context.ActionArguments.Values
ExceptionContext.ModelState
可以为您提供模型状态字典。你能从中得到你想要的吗?ModelState
基本上是一个IDictionary
我无法从中分辨出操作参数的类型。另外,Modelstate
只有在存在模型绑定问题时才会包含某些内容。mvc是否为每个请求创建一个筛选器对象?如果不是的话,在多线程环境中这么多的请求不会发生冲突吗?看看这个答案,看起来这不是一个正确的方法
public class ExceptionFilter : IExceptionFilter, IActionFilter
{
private IDictionary<string, object> _actionArguments;
public void OnException(ExceptionContext context)
{
_actionArguments // great success!
}
public void OnActionExecuting(ActionExecutingContext context)
{
_actionArguments = context.ActionArguments;
}
public void OnActionExecuted(ActionExecutedContext context)
{
// do nothing
}
}