How to get current model in action filter

前端 未结 3 1384
粉色の甜心
粉色の甜心 2020-12-17 10:25

I have a generic action filter, and i want to get current model in the OnActionExecuting method. My current implementation is like below:

public         


        
3条回答
  •  粉色の甜心
    2020-12-17 11:03

    In case your controller action has multiple arguments and in your filter you want to select the one that is bound via [FromBody], then you can use reflection to do the following:

    public void OnActionExecuting(ActionExecutingContext context)
    {
        foreach (ControllerParameterDescriptor param in context.ActionDescriptor.Parameters) {
            if (param.ParameterInfo.CustomAttributes.Any(
                attr => attr.AttributeType == typeof(FromBodyAttribute))
            ) {             
                var entity = context.ActionArguments[param.Name];
    
                // do something with your entity...
    

提交回复
热议问题